Javascript data structure for fast lookup and ordered looping?

is there a data structure or a pattern in Javascript that can be used for both fast lookup (by key, as with associative arrays) and for ordered looping?

Right, now I am using object literals to store my data but I just disovered that Chrome does not maintain the order when looping over the property names.

Is there a common way to solve this in Javascript?

Thanks for any hints.

Create a data structure yourselves. Store the ordering in an array that is internal to the structure. Store the objects mapped by a key in a regular object. Let’s call it OrderedMap which will have a map, an array, and four basic methods.


    set(key, value)

function OrderedMap() { = {};
    this._array = [];

When inserting an element, add it to the array at the desired position as well as to the object. Insertion by index or at the end is in O(1).

OrderedMap.prototype.set = function(key, value) {
    // key already exists, replace value
    if(key in {[key] = value;
    // insert new key and value
    else {
        this._array.push(key);[key] = value;

When deleting an object, remove it from the array and the object. If deleting by a key or a value, complexity is O(n) since you will need to traverse the internal array that maintains ordering. When deleting by index, complexity is O(1) since you have direct access to the value in both the array and the object.

OrderedMap.prototype.remove = function(key) {
    var index = this._array.indexOf(key);
    if(index == -1) {
        throw new Error('key does not exist');
    this._array.splice(index, 1);

Lookups will be in O(1). Retrieve the value by key from the associative array (object).

OrderedMap.prototype.get = function(key) {

Traversal will be ordered and can use either of the approaches. When ordered traversal is required, create an array with the objects (values only) and return it. Being an array, it would not support keyed access. The other option is to ask the client to provide a callback function that should be applied to each object in the array.

OrderedMap.prototype.forEach = function(f) {
    var key, value;
    for(var i = 0; i < this._array.length; i++) {
        key = this._array[i];
        value =[key];
        f(key, value);

See Google’s implementation of a LinkedMap from the Closure Library for documentation and source for such a class.

Read More:   Why use javascript:void(0) instead of # in href? [duplicate]

The only instance in which Chrome doesn’t maintain the order of keys in an object literal seems to be if the keys are numeric.

  var properties = ["damsonplum", "9", "banana", "1", "apple", "cherry", "342"];
  var objLiteral = {
    damsonplum: new Date(),
    "9": "nine",
    banana: [1,2,3],
    "1": "one",
    apple: /.*/,
    cherry: {a: 3, b: true},
    "342": "three hundred forty-two"
  function load() {
    var literalKeyOrder = [];
    for (var key in objLiteral) {

    var incremental = {};
    for (var i = 0, prop; prop = properties[i]; i++) {
      incremental[prop] = objLiteral[prop];

    var incrementalKeyOrder = [];
    for (var key in incremental) {
    alert("Expected order: " + properties.join() +
          "\nKey order (literal): " + literalKeyOrder.join() +
          "\nKey order (incremental): " + incrementalKeyOrder.join());

In Chrome, the above produces: “1,9,342,damsonplum,banana,apple,cherry”.

In other browsers, it produces “damsonplum,9,banana,1,apple,cherry,342”.

So unless your keys are numeric, I think even in Chrome, you’re safe. And if your keys are numeric, maybe just prepend them with a string.

has been noted, if your keys are numeric
you can prepend them with a string to preserve order.

var qy = {
  _141: '256k AAC',
   _22: '720p H.264 192k AAC',
   _84: '720p 3D 192k AAC',
  _140: '128k AAC'


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts