V8 Object limits

219 views
Skip to first unread message

Timothy Caswell

unread,
Aug 24, 2010, 6:30:35 PM8/24/10
to nod...@googlegroups.com
So I made this simple script to test the property insertion speed of plain js objects:

var sys = require('sys');
var data = {};

var start = Date.now();
var next = start + 100;
var i = 1;
var last = 0;
var lastT = start;
while (true) {
  data[i
++] = new Object();
 
var d = Date.now();
 
if (d > next) {
    next
= d + 100;
    sys
.error(i + ": " + (d - lastT) + "ms at " + (Math.floor((i - last) / (d - lastT) * 1000)) + "/second");
    last
= i;
    lastT
= d;
   
  }
}

The output is as follows on my laptop:

283357: 106ms at 2673179/second
490082: 111ms at 1862387/second
661235: 119ms at 1438260/second
1058083: 101ms at 3929188/second
1400750: 215ms at 1593800/second
1877345: 122ms at 3906516/second
2163314: 288ms at 992947/second
2599652: 127ms at 3435732/second
3028608: 127ms at 3377606/second
3171592: 354ms at 403909/second
3518964: 101ms at 3439326/second
3879687: 101ms at 3571514/second
4220126: 131ms at 2598770/second
4538423: 101ms at 3151455/second
4649079: 559ms at 197953/second
4982686: 109ms at 3060614/second
5268654: 104ms at 2749692/second
5554624: 105ms at 2723523/second
5840594: 107ms at 2672616/second
6126564: 106ms at 2697830/second
6412534: 108ms at 2647870/second
6698504: 844ms at 338827/second
6889124: 153ms at 1245882/second
7175091: 129ms at 2216798/second
7461062: 120ms at 2383091/second
7747032: 120ms at 2383083/second
8033002: 122ms at 2344016/second
8318973: 1049ms at 272612/second
8652580: 127ms at 2626826/second
8938548: 123ms at 2324943/second
9224518: 127ms at 2251732/second
9510488: 126ms at 2269603/second
9796458: 127ms at 2251732/second
10082428: 128ms at 2234140/second
10236219: 185ms at 831302/second
10368392: 1257ms at 105149/second
10702000: 147ms at 2269442/second
10987968: 143ms at 1999776/second
11273938: 144ms at 1985902/second
11559908: 150ms at 1906466/second
11845878: 148ms at 1932229/second
12131848: 150ms at 1906466/second
12417818: 150ms at 1906466/second
12703788: 151ms at 1893841/second
12989758: 151ms at 1893841/second
13132741: 1863ms at 76748/second
13191442: 101ms at 581198/second
13247784: 1961ms at 28731/second
13268706: 1882ms at 11116/second
13272510: 1863ms at 2041/second
13273202: 1853ms at 373/second
13273328: 1837ms at 68/second
13273351: 1839ms at 12/second
13273355: 1838ms at 2/second
13273356: 1842ms at 0/second
13273357: 1838ms at 0/second


You'll notice that it starts out nice and fast at 2,673,179  inserts per second, but by the time it reaches about 13,273,357  properties, it's down to taking almost 2 seconds to insert a single property.  Also I'm seeing some blips in my graph that I assume are the stop-the-world garbage collector.  

Does anyone know what are the performance characteristics of V8 objects/arrays and what number of keys we should limit our objects.  I don't mind creating my own trees of objects to keep the number of properties in a single object low.  I just don't where to put this threshold and why or under what conditions.

Could I get some insight from some people on this list?

Ryan Gahl

unread,
Aug 24, 2010, 6:43:30 PM8/24/10
to nod...@googlegroups.com
Does this actually test "property insertion" or just plain old "object creation".

If the purpose is the former, why not just create 1 object above the loop to use as the value for all properties? Then I imagine the results will be wildly different.

If the true purpose of the test is object creation though, I am by no means going to be able to help as I've done no work on benchmarking in those extremes.  

Just wanted to point out that your test may or may not be accurate depending on what you're really trying to test. I assume your wording was just wrong, and you really wanted to test object creation though.




--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

Timothy Caswell

unread,
Aug 24, 2010, 6:48:07 PM8/24/10
to nod...@googlegroups.com
Oops, my test had a shared object before, but I was experimenting with creating a new object each time. I posted the wrong version.  


You're right, the behavior is fairly different.  It goes on to 51,623,866 inserts before running out of memory and crashing with "FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory"

Ryan Gahl

unread,
Aug 24, 2010, 7:06:33 PM8/24/10
to nod...@googlegroups.com
So it actually looks like a roughly similar curve to me, albeit with a dilated timeline. I assume given enough available memory, this test would also approach 0/sec. 

I feel like this isn't so much a V8 or "properties per object" issue, but just bound to memory & CPU. 

What happens if you "shard" this test so that after every so many (maybe 10million to start, maybe 1million for another test, then 100k, etc...) insertions, it uses a new object? Are you then able to exceed the total number of properties represented by this "1 object test", or is it roughly the same. If roughly the same, this is probably not an upper bound of property insertions, but just system resources. At the same time as testing total properties over all test objects, does the introduction of multiple objects have any effect on the speed curve? If yes, well then yeah there would appear to be something apparently suboptimal with V8's algorithm for adding properties. If the number of objects and properties per object still have no real effect on the curve as the totals increase... back to this just being (probably) just  a system resources-bound type problem (for total properties as well as insertion speed)... 

Vitali Lovich

unread,
Aug 24, 2010, 7:54:40 PM8/24/10
to nod...@googlegroups.com
Remember that the way v8 works, if you keep adding properties as opposed to declaring them up-front, it'll keep creating layers of hidden classes which is suboptimal (both in terms of accessing them & how much memory you use).  I forget how many levels of hidden classes it goes for before it switches to a different algorithm.

Erik Corry

unread,
Aug 25, 2010, 4:28:59 PM8/25/10
to nod...@googlegroups.com
2010/8/25 Vitali Lovich <vlo...@gmail.com>:

> Remember that the way v8 works, if you keep adding properties as opposed to
> declaring them up-front, it'll keep creating layers of hidden classes which
> is suboptimal (both in terms of accessing them & how much memory you use).
>  I forget how many levels of hidden classes it goes for before it switches
> to a different algorithm.

Me too, but you can be sure that it is substantially less than 51,623,866 :-)

Vitali Lovich

unread,
Aug 25, 2010, 5:29:50 PM8/25/10
to nod...@googlegroups.com
My guess is 51,623,865 :D
Reply all
Reply to author
Forward
0 new messages