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

JavaScript performance comparison

Revision 2 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" : 10000,
        "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);
  var 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);
  var n = keys.length;
  while (n--) {
    var key = keys[n];
    obj[key.toLowerCase()] = obj[key]
    delete obj[key]
  }
pending…
new object
var keys = Object.keys(obj);
var n = keys.length;
var newobj={}
while (n--) {
  key = keys[n];
  newobj[key.toLowerCase()] = obj[key];
}
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