1

[UPDATE The discussion of "slow mode" below also pointed me to Why the convertToFastObject function make it fast?, which has a nice discussion of the V8 internal discussion that @benjamin-gruenbaum referenced.]

I was wondering about the performance of JavaScript one for optimizing reads, so I wrote a JSPerf test suite, and the results are non-intuitive.

Firstly, the question "Performance of key lookup in JavaScript object" discusses the internals of V8s object creation, but I don't see why the method of creation would change the read performance after it has been created.

Overview

I want to test read access on various data structures. I chose 3 structures:

  1. An object literal, created and assigned in one statement
  2. An object literal, created in one statement, then values assigned in subsequent statements
  3. An array literal, created in one statement, but populated as an associative array, which presumably gets translated to an object in the background.

Test

http://jsperf.com/object-read-varies-by-creation-method

Methodology

In the setup method. I create 3 data structures and populate them with about random words from /usr/share/dict/words, in randomized order (but the same order on each test run). Each test case retrieves the same 10 random words from /usr/share/dict/words, but not necessarily words that are in the list, to simulate misses as well as hits.

Results

What I see on most browsers is pretty intuitive: the two objects perform better than the pseudo-associative array. My assumption is that the array literal is being proxied by an object in those cases, increasing the overall time it takes to access.

What I don't get is the Chrome 33 case, where creating and assigning an object as a single statement performs dramatically better than populating the structure after creation.

If the test included setup time, I could understand that difference, but as it stands, I don't fully get why the two object literals don't perform about the same.

The best guess I've come up with so far is that Chrome's object creation algorithm is able to optimize its hash tree better during an act of creation that includes all keys at once then it is when it's inserting the keys one at a time after the fact.

Can anyone more familiar with these structures account for the difference?

Results chart

Community
  • 1
  • 1
Palpatim
  • 9,074
  • 35
  • 43
  • Fun side note - Chrome deopts if you have too many properties on an object. Now, why are you writing a micro benchmark anyway? What's the real issue? Even the slowest of those alternatives (the benchmark isn't that great but not the point) can do millions of iterations every second. Are you sure this is where your bottleneck is? – Benjamin Gruenbaum Mar 21 '14 at 18:22
  • As for 'internals' - every object in v8 has an 'array' store location where it can store array like elements fast. Using an array is incorrect here (as in your h sample) but not much slower. When you have many properties on an object it will fall back to hashset mode which is considerably slower than fast mode. – Benjamin Gruenbaum Mar 21 '14 at 18:25
  • I just checked - It's 1022, you have 2044 properties so your object is in slow mode, if you're using it just for lookup you should use a `Set` and not an object by the way. – Benjamin Gruenbaum Mar 21 '14 at 18:31
  • Thanks for the notes. This is mostly an exercise to help me understand the tradeoffs in each structure, and the discrepancy in Chrome was confusing. As far as using `Set`, I'll pass for now, since browser support is still lacking. (And although it's by far the fastest on Firefox, it's much slower on Chrome, even with the object in "slow" mode.) All in all, you're right, this level of performance would be fine for a user-centered operation, but if I have JS on the server, I may need to process bulk data, and I could see a use case like this coming up. Thanks again for the discussion. – Palpatim Mar 21 '14 at 18:58
  • @BenjaminGruenbaum millions per second is not that much for something low level like property read. – Esailija Mar 23 '14 at 16:19

0 Answers0