Why is frozen "listing" slower?

To access the data in the array, I created an enum variable to have human-readable identifiers for the fields.

 var columns = { first: 0, second: 1 }; var array = ['first', 'second']; var data = array[columns.first]; 

When I found out about Object.freeze , I wanted to use this for enumeration so that it could not be changed, and I expected the virtual machine to take advantage of this information.

As it turns out, tests become slower in Chrome and Node, but slightly faster in Firefox (compared to direct access by number).

The code is available here: http://jsperf.com/array-access-via-enum

Here are the tests from Node (corresponding to the JSPerf tests):

  fixed Number: 12ms enum: 12ms frozenEnum: 85ms 

Does V8 not yet have a great implementation, or is there something suboptimal with this approach for my use?

+6
source share
2 answers

I tried your test in Firefox 20, which is significantly faster in all directions, and IE 10, which is slightly faster and more consistent.

So my answer is No, V8 doesn't have much implementation yet

+4
source

According to this bugreport , freezing an object currently puts it in dictionary-mode, which is slow.

Therefore, instead of improving performance, it becomes a certain slowdown for "enumerations" / small arrays.

+1
source

All Articles