set vs push

JavaScript performance comparison

Test case created by Joshua T Kalis

Info

setting .length element in array instead of pushing value onto the stack.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var count = 100000,
        len = 0,
        testArray = [];
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
set
while (count > testArray.length) {
  testArray[testArray.length] = 9;
}
pending…
set with len cache
while (count > len) {
  testArray[len] = 9;
  len = testArray.length;
}
pending…
push
while (count > testArray.length) {
  testArray.push(9);
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment