Why is a frozen "enum" slower?

Problem

In order to access the data in an array I created an enum-like variable to have human readable identifiers to 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 the enum so that it can not be changed, and I expected the VM to use this info to its advantage.

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

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

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

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

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

Problem courtesy of: Timm

Solution

I tried your test in Firefox 20, which is massively faster across the board, and IE 10 which slightly faster and more consistant.

So my answer is No, V8 does not yet have a great implementation

Solution courtesy of: Colin Pickard

Discussion

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

So instead of improving the performance, it becomes a definite slowdown for "enums"/small arrays.

Discussion courtesy of: Timm

This recipe can be found in it's original form on Stack Over Flow.