• Content count

  • Joined

  • Last visited

Community Reputation

429 Neutral

About SacredPixel

  • Rank
  1. 12 Tricks to Selling Your Ideas, Your Game & Yourself

    Awesome article, I definitely got "closed"!
  2. Thanks, Please let me know what areas could be improved.     Good point, I think this could be an article on its own but I'll try to add more info here     Seems this technique is what most compiled Asm.js code use. By using int for storage enables browsers to convert code directly into assembly and it also gets rid of GC. I don't know if its true outside of Asm.js domain but using int is generally faster than strings or objects.
  3. Recent versions of JavaScript engines are designed to execute large bodies of code very fast but if you don't know how JavaScript engines work internally you could easily degrade your application's performance. It's specially true for games that need every drop of performance they can get. In this article I will try to explain some common optimization methods for JavaScript code that I picked up during development of my projects. Garbage Collection One of the biggest problems in having a smooth experience resides in JavaScript garbage collector(GC) pauses. In JavaScript you create objects but you don't release them explicitly. That's job of the garbage collector. The problem arises when GC decides to clean up your objects: execution is paused, GC decides which objects are no longer needed and then releases them. Zig-zag memory usage pattern while playing a JavaScript game. To keep your framerate consistent, you should keep garbage creation as low as possible. Often objects are created with the new keyword e.g. new Image() but there are other constructs that allocate memory implicitly: var foo = {}; //Creates new anonymus object var bar = []; //Creates new array object function(){} //Creates new function You should avoid creating objects in tight loops (e.g. rendering loop). Try to allocate objects once and reuse them later. In languages like C++ sometimes developers use object pools to avoid performance hits associated with memory allocation and fragmentation. The same idea could be used in JavaScript to avoid GC pauses. Here you can find out more about object pools. To better demonstrate implicit garbage creation, consider following function: function foo(){ //Some calculations return {bar: "foo"}; } Each time this function is called it creates a new anonymous object that needs to be cleared at some point. Another performance hit comes from using array shorthand [] to clear your array: var r = new Array("foo", "bar"); //New array filled with some values, same as ["foo", "bar"] r = [];//Clear the array As you can see, the second line creates a new array and marks the previous one as garbage. It's better to set the array length to 0: r.length = 0; Functions can wake up the GC as well. Consider the following: function foo(){ return function(x, y){ return x + y; }; } In above code we return a function refrence from our foo function but we also allocate memory for our anonymous function. The above code could be rewritten to avoid GC: function bar(x, y){ return x + y; } function foo(){ return bar; } An important thing to be aware of is that global variables are not cleaned up by the garbage collector during the life of your page. That means objects like the above functions are only created once so whenever possible use them to your advantage. Also globals are cleaned up when users refresh the page, navigate to another page or close your page. These are straightforward ways for avoiding performance hits that come from GC but you should also be aware of other JavaScript library functions that may create objects. By knowing what values are returned from your library functions you could make better decisions about designing your code. For example, if you know that a library function may allocate memory and you use that function in a performance-critical section of your code you may want to rewrite or use a similiar but more effecient function. JavaScript Internals JavaScript engines do some preparation on your code (including some optimizations) before execution. Knowing what they do behind the scenes will enable you to write generally better code. Here is an overview of how two popular JavaScript engines (Google's V8 and Mozilla's SpiderMonkey) work under the hood: V8: JavaScript is parsed and native machine code is generated for faster execution. The initial code is not highly optimized. A runtime profiler monitors the code being run and detects "hot" functions (e.g. code that runs for long time). Code that's flagged as "hot" will be recompiled and optimized. V8 can deoptimize previousely optimized code if it discovers that some of the assumptions it made about the optimized code were too optimistic. Objects in V8 are represented with hidden classes to improve property access. SpiderMonkey: JavaScript is parsed and bytecode is generated. A runtime profiler monitors the code being run and detects "hot" functions (e.g. code that runs for long time). Code that's flagged as "hot" will be recompiled and optimized by Just-In-Time(JIT) compiler. As you can see, both engines and other similiar engines applies common optimizations that we can take advantage of. Deleting Object Properties Avoid using the delete keyword for removing object properties if you can. Consider the following code: var obj = { foo: 123 }; delete; typeof == 'undefined' //true It will force V8 to change obj's hidden class and run it on a slower code path. Same is true about other JavaScript engines that optimize for "hot" objects. If possible, it's better to null the properties of object instead of removing them. Monomorphic Variables Whenever possible try to keep your variables monomorphic. For example don't put different objects with different hidden classes in your arrays. Same applies to properties and function parameters. Functions that are supplied with constant parameter types perform faster than the ones with different parameters. //Fast //JS engine knows you want an array of 3 elements of integer type var arr = [1, 2, 3]; //Slow var arr = [1, "", {}, undefined, true]; The below function can be called with different parameter types(ints, strings, objects, etc) but it will make it slow: function add(a, b){ return a + b; } //Slow add(1, 2); add('a', 'b'); add(undefined, obj); Array of Numbers Using an array is usually faster than accessing object properties. This is particularly beneficial when the array contains numbers. For example it's better to write vectors using arrays than with objects with x, y, z properties. Arrays with Holes Avoid "holes" in your arrays. It will make things slower than it should. Holes are created by deleting elements or adding elements out of range of the array. For example: var arr = [1, 2, 3, 4, 5];//Full array delete arr[0];//Creates hole arr[7] = 1; //Creates hole var hArr = [0, 1, 2, 3, /* hole */, 5];//Holey array Pre-allocating Large Arrays Current implementations of SpiderMonkey and V8 favor growing over pre-allocating large arrays (with more than 64K elements). Keep in mind that this is largely implementation-dependent as some implementations such as Nitro(Safari) or Carakan(Opera) favors pre-allocated arrays. Object Declaration I can't give you single method for creating objects as it's very engine-dependent but you can see current performance test reults for yourself here and decide what's best for your application. Integer Arithmetic Use integers where possible. Because most programs use integers, modern JavaScript engines are optimized for integer operations. In JavaScript all numbers are Number type thus you can't directly specify storage type (int, float, double, etc) like other strongly typed languages. If your application is math heavy, one unintended floating point artimetic can degrade your application performance and it can spread through your application. For example: function halfVector(v){ v[0] /= 2; v[1] /= 2; v[2] /= 2; } var v = [3, 5, 9]; halfVector(v); In a strong typed language like C++ where we used int type we would get [1, 2, 4] as result but in our case we implicitly switched to floating point math in our code. JavaScript engines use integer math operations where possible and its because modern processors execute integer operations faster than floating point operations. Unlike other objects and floating point values, common integer values are stored in memory where they don't require allocation. To tell JavaScript engine we want to store integer values in our array in above example we could use bitwise or operator: function halfIntVector(v){ v[0] = (v[0] / 2) | 0; v[1] = (v[1] / 2) | 0; v[2] = (v[2] / 2) | 0; } Result of the bitwise or operator is an integer, this way JavaScript engine knows that it should not allocate memory. Floating Point Values As stated in previous point, any time a floating point number is assigned to an object property or array element a memory is allocated. If your program does lots of floating point math these allocations can be costly. Although you can't avoid allocation for object properties you can use typed arrays (Float32Array and Float64Array). Typed arrays can only store floating point values and JavaScript runtime can access and store the values without memory allocations. Conclusion There are many other ways to optimize your code but I think this should be enough to get started. Just keep in mind that you should always profile your code and optimize for the portion that takes the most time to execute. Article Update Log 19 Dec 2013: Updated the article with some tips for integer/float values 12 Dec 2013: Initial release
  4. Animation Blending

    wow! :)   So for best possible transitions we must have intermidiate animations that's triggered in right time. I think I'll have to use the second approach more to reduce the number of intermidiate animations that have to be created.   Thanks again for your time haegarr, much appreciated!
  5. Animation Blending

    Thanks haegarr for your explanation. Yes I'm using weighting formula like yours:   ( 1 - w ) * A1 + w * A2   I'm not sure if I stated my question correctly but what I'm looking for is to somehow transition between animations so the player doesn't notice animation change while requesting new animations (3 or more, take a look at example below). We have animation channels so separating bones for different animations isn't an issue.   Say the player character is walking and then starts running. Our current animation code would transition the walk to run animation in 300 milliseconds using the above formula to adjust the weight toward the run animation. So far so good. But the problem starts when user request another animation like crouch in middle of walk to run transition.
  6. Animation Blending

    Hello Everyone I'm working on a multiplayer FPS game that is fast paced. We have skeletal animation code in place and we can blend from one animation to other when player change actions (like changing from running animation to walking animation). However the problem is that the player can request another animation track (or any number of tracks if she/he is fast enough) while other two animations are blending. I can't simply change new animation with other two while they're still blending. My initial thoughts was to use a blending weight that takes into consideration previous animations and new one but I don't know how I could implement that or even if its correct. I really appreciate it if anyone can tell me how other games accomplish this task or any pointers in right direction. Thanks
  7. Slightly amibitious I know

    [quote name='robobrain' timestamp='1320380202' post='4880372'] Go with html5/JavaScript to start with: [url=""][/url] You don't need to learn server scripting unless you need persistence. It's more complicated. [/quote] Html5/Canvas is nice but you should keep it in mind that only new versions of browsers support it. Unless you want your games to be only one session long and don't want to save any data (highscores, player items, etc) you're fine without a server scripting language. Also learning a server scripting language like PHP is not harder than learning JavaScript.
  8. Slightly amibitious I know

    Like what josh suggested, for a browser game you need to know some server side scripting language like PHP, ASP.NET, Ruby etc. Also you need to learn some SQL in order to store your game data. And for animations and chat some JavaScript is required ( You could also use JavaScript libraries like jQuery, YUI, etc ). Ah i almost forgot: I don't know about any online resource that teach how to create a browser game but here are some tutorials for mentioned tools to get you started: [url=""]PHP 101[/url] [url=""][/url] website contains tutorials for both web-forms and MVC [url=""]SQL Course[/url] [url=""]JavaScript[/url] on, its more reference than tutorial but still useful HTH