JavaScript's Statement Performance Questions
I've always assumed that since (x&1) is a bitwise operation, it would be the fastest way to check for even/odd numbers, rather than checking for the remainder of the number.
I'd suggest you code a simple script like:
for(var i = 0; i < 1000; i++){
// Test your code here.
}
You can benchmark whatever you want that way, possibly adding timing functions before and after the for
statement to be more accurate.
Of course you'll need to tweak the upper limit (1000 in this example) depending on the nature of your operations - some will require more iterations, others less.
Performance characteristics for all browser (especially at the level of individual library functions) can vary dramatically, so it's difficult to give meaningful really meaningful answers to these questions.
Anyhoo, just looking at the fast js engines (so Nitro, TraceMonkey, and V8)
[ ]
will be faster thannew Array
--new Array
turns into the following logiccons
= lookup property "Array", if it can't be found, throw an exception- Check to see if
cons
can be used as a constructor, if not: throw an exception thisVal
= runtime creates a new object directlyres
= result of callingcons
passingthisVal
as the value forthis
-- which requires logic to distinguish JS functions from standard runtime functions (assuming standard runtime functions aren't implemented in JS, which is the normal case). In this caseArray
is a native constructor which will create and return a new runtime array object.- if
res
is undefined or null then the final result isthisVal
otherwise the final result isres
. In the case of callingArray
a new array object will be returned andthisVal
will be thrown away
[ ]
just tells the JS engine to directly create a new runtime array object immediately with no additional logic. This meansnew Array
has a large amount of additional (not very cheap) logic, and performs and extra unnecessary object allocation.newlist[newlist.length] = ...
is faster (esp. if newlist is not a sparse array), but push is sufficiently common for me to expect engine developers to put quite a bit of effort into improving performance so this could change in time.If you have a tight enough loop there may be a very slight win to the ternary operator, but arguably that's an engine flaw in the trival case of
a = b ? c : d
vsif (b) a = c; else a = d
Just the function call overhead alone will dwarf the cost of more or less any JS operator, at least in the sane cases (eg. you're performing arithmetic on numbers rather than objects)
The
foreach
syntax isn't yet standardised but its final performane will depend on a large number of details; Often JS semantics result in efficient looking statements being less efficient -- eg.for (var i in array) ...
is vastly slower thanfor (var i = 0; i < array.length; i++) ...
as the JS semantics requirein
enumeration to build up a list of all properties on the object (including the prototype chain), and then checking to make sure that each property is still on the object before sending it through the loop. Oh, and the properties need to be converted from integers (in the array case anyway) into strings, which costs time and memory.