Map an object's keys to an array (v5)

Revision 5 of this benchmark created by molokoloco on


Description

http://jsfiddle.net/molokoloco/HhnZL/

http://kangax.github.com/es5-compat-table/ : Object.keys()

There an other difference between this function : $.map() et Object.keys() do a test and don't give back own Object properties :

var dimensions = {width: 10, height: 15}; var arr1 = $.map(dimensions, function(value, index) { return index; }); console.log(arr1); //> ["width", "height"] Object.prototype.properties = function() { var p = []; for (var k in this) p.push(k); return p; }; arr2 = dimensions.properties(); console.log(arr2); //> ["width", "height", "properties"]

Preparation HTML

<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>

Setup

var dimensions = {};
    for (var i = 0; i < 1000; i++) dimensions[i] = i;
    
    Object.prototype.properties = function() {
        var p = [];
        for (var k in this) p.push(k);
        return p;
    };
    
    var properties = function(obj) {
        var p = [];
        for (var k in obj) p.push(k);
        return p;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
$.map(dimensions, function(){})
var arr = $.map(dimensions, function(value, index) {
    return index;
});
ready
Object.prototype.properties
var arr = dimensions.properties();
ready
var properties = function(obj)
var arr = properties(dimensions);
ready
Object.keys(dimensions)
var arr = Object.keys(dimensions);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 5: published by molokoloco on