3

I was watching a JS video by Douglas Crockford and he mentioned a way to "push" an item into an array, without using the Array.push method. It's super simple and something I had never thought of doing, but after doing running a speed test I realize it's slower than Array.push, which doesn't make much sense to me.

How can something like Array.push (which does the same thing as array[array.length] = val, plus other things along with the cost of calling the function itself), be faster than just a plain expression?

Maybe my tests are bad, maybe I have a lack of CS fundamentals here, but if someone can explain the speed differences here I'd really appreciate that. I was simply trying to see if array[array.length] = val was a faster alternative to Array.push, but apparently not. g

This is how Array.push is implemented:

function push(value) {
    var len = this.length;
    this[len] = value;
    len++;
    this.length = len;
    return len;
}

As you can see, it clearly has much more logic then a simple expression, yet it's faster.

Take a look:

let arr = [],
    p1 = performance.now();

for (let i = 1, len = 10; i <= len; i++) {
    arr[arr.length] = i;
}

let p2 = performance.now();

console.log(p2 - p1);

Running the example above, I get 0.015000000001236913, or 0.015 for short.

Now let's look at the Array.push:

let arr = [],
    p1 = performance.now();

for (let i = 1, len = 10; i <= len; i++) {
    arr.push(i);
}

let p2 = performance.now();

console.log(p2 - p1);

Running the example above, I get 0.010000000002037268, or 0.010 for short.

Array.push is 0.010, and array[array.length] = val is 0.015, using the same loop.

How can this be? It makes absolutely no sense to me. What am I missing here?

Lansana Camara
  • 9,497
  • 11
  • 47
  • 87
  • 1
    Here is the comprehensive test and results for your reference - http://blog.scottlogic.com/2010/10/15/javascript-array-performance.html – David R Aug 24 '16 at 18:05
  • What does `performance.now()` do? – Ibu Aug 24 '16 at 18:08
  • Awesome, thanks for the reference. It's funny that the only browser `Array.push` is slower than `array[array.length] = val` is Chrome, which is the one I tested on :P. – Lansana Camara Aug 24 '16 at 18:09
  • Different browsers compile/transpile/execute JS code differently. Are you only interested in Chrome? If so, we'll have to dive into V8 to see what it's actually doing. What operating system you are on probably affects this as well. – Matthew Herbst Aug 24 '16 at 18:13
  • 1
    @Ibu `performance.now()`: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now – Lansana Camara Aug 24 '16 at 18:13
  • @MatthewHerbst I hadn't thought of the fact that the browser/me using ES6 could make a difference, but now I see that. – Lansana Camara Aug 24 '16 at 18:15
  • "*This is how Array.push is implemented:*" - how do you know? – Bergi Aug 24 '16 at 18:15
  • @Bergi Honestly, I don't know for sure. I found that online. It's all hearsay. – Lansana Camara Aug 24 '16 at 18:18
  • 1
    How often did you run these tests? Did you try them in different orders? Did you do warmups? And clearly a loop with 10 iterations, measuring about 10µs, is hardly significant. – Bergi Aug 24 '16 at 18:19
  • Well I also did a similar test without a loop, but ten times and still each time the Array.push variant was faster. I suppose many factors come into play, but it still just doesn't make any sense to me how a function that uses the expression (or something similar), is faster than the expression itself. And I understand the insignificance here, but it still doesn't change the fact :P – Lansana Camara Aug 24 '16 at 18:21
  • A loop iterating 10 times is not giving you a clear picture. – Ibu Aug 24 '16 at 18:22
  • You should watch https://www.youtube.com/watch?v=g0ek4vV7nEA :-) – Bergi Aug 24 '16 at 18:22
  • Thanks for the vid! New to the whole performance thing so it's very helpful. – Lansana Camara Aug 24 '16 at 18:24

0 Answers0