2

I did some benchmarking on my setup (i7 @ 3.07GHz, 6GB ram) with the Object.keys method and noticed some interesting results.

This is the code I tried:

var foo = {};

for (var i = 0; i < 600000; i++) {
    foo[i] = true;
}

console.time("foobar");

var keys = Object.keys(foo);

console.timeEnd("foobar");

I replaced xxx with different values, starting at 10 000 and going up to 1 000 000. When I came to 400k execution-time was around 121ms, 500k ~130ms. But then at 600k it jumped to 250ms. Why does it do this?

user3054852
  • 149
  • 1
  • 10

1 Answers1

1

You can examine how much memory you object foo uses.

  1. When i = 600000 => foo object occupies around 2.4 MB

  2. When i = 500000 => foo object occupies around 2MB

So, your CPU cache size is probably around 2MB, and when you need more memory than it can handle, performance degrades non-linearly (as it was linear with memory consumption less than 2 MB)

Community
  • 1
  • 1
Andrei Karpushonak
  • 8,338
  • 1
  • 36
  • 50