What's the most efficient way to turn an object's keys to its lowercase version

JavaScript performance comparison

Revision 3 of this test case created

Info

http://stackoverflow.com/questions/12539574/whats-the-best-way-most-efficient-to-turn-all-the-keys-of-an-object-to-lower

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {
      "SomeThing": 2,
      "OthErThing": 9,
      "AnotherThing": 'fsdfsfd',
      "OhAndOneMoreThing": 193013923,
      "aQuiteLargeStringaQuiteLargeStringaQuiteLargeString": 29320381,
      "SomeThing2": 2,
      "OthErThing2": 9,
      "AnotherThing2": 10000,
      "OhAndOneMoreThing2": 193013923,
      "aQuiteLargeStringaQuiteLargeStringaQuiteLargeString2": 29320381,
      "SomeThing3": 2,
      "OthErThing3": 9,
      "AnotherThing3": 10000,
      "OhAndOneMoreThing3": 193013923,
      "aQuiteLargeStringaQuiteLargeStringaQuiteLargeString3": 29320381,
      "SomeThing4": 2,
      "OthErThing4": 9,
      "AnotherThing4": 10000,
      "OhAndOneMoreThing4": 193013923,
      "aQuiteLargeStringaQuiteLargeStringaQuiteLargeString4": 29320381,
      "SomeThing5": 2,
      "OthErThing5": 9,
      "AnotherThing5": 10000,
      "OhAndOneMoreThing5": 193013923,
      "aQuiteLargeStringaQuiteLargeStringaQuiteLargeString5": 29320381
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
joaojeronimo's function
var keys = Object.keys(obj),
    n = keys.length;
while (n--) {
  var key = keys[n];
  if (key !== key.toLowerCase()) {
    obj[key.toLowerCase()] = obj[key]
    delete obj[key]
  }
}
pending…
Don't check if the key is already in lower case
// Might be cheaper to just change it, but I doubt it
var keys = Object.keys(obj),
    n = keys.length,
    key;
while (n--) {
  key = keys[n];
  obj[key.toLowerCase()] = obj[key]
  delete obj[key]
}
pending…
new object
var keys = Object.keys(obj),
    n = keys.length,
    newobj = {};

while (n--) {
  key = keys[n];
  newobj[key.toLowerCase()] = obj[key];
}
pending…
without Object.keys
var newobj = {};
for (var key in obj) {
  newobj[key.toLowerCase()] = obj[key];
}
pending…
var lowerCache = {};

FN = function(obj) {
  if (typeof(obj) === "string" || typeof(obj) === "number") return obj;

  var l = obj.length;
  if (l) {
    l |= 0;
    var result = [];
    result.length = l;
    for (var i = 0; i < l; i++) {
      var newVal = obj[i];
      result[i] = typeof(newVal) === "string" ? newVal : FN(newVal);
    }
    return result;
  } else {
    var ret = {};
    for (var key in obj) {

      var keyStr = typeof(key) === "string" ? key : String(key);
      var newKey = lowerCache[keyStr];
      if (newKey === undefined) {
        newKey = keyStr.toLowerCase();
        lowerCache[keyStr] = newKey;
      }

      var newVal = obj[key];
      ret[newKey] = typeof(newVal) === "string" ? newVal : FN(newVal);
    }
    return ret;
  }
};

FN(obj);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment