Editing What's the most efficient way to turn an object's keys to its lowercase version This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) http://stackoverflow.com/questions/12539574/whats-the-best-way-most-efficient-to-turn-all-the-keys-of-an-object-to-lower Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) 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 } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code 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] } } Test 2 Title Async (check if this is an asynchronous test) Code // 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] } Test 3 Title Async (check if this is an asynchronous test) Code var keys = Object.keys(obj), n = keys.length, newobj = {}; while (n--) { key = keys[n]; newobj[key.toLowerCase()] = obj[key]; } Test 4 Title Async (check if this is an asynchronous test) Code var newobj = {}; for (var key in obj) { newobj[key.toLowerCase()] = obj[key]; } Test 5 Title Async (check if this is an asynchronous test) Code 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);