Object.keys vs _,keys vs hasOwnProperty (v54)

Revision 54 of this benchmark created on


Preparation HTML

<script src="http://underscorejs.org/underscore-min.js"></script>

Setup

var i;
    var key;
    var keys;
	var keySet;
    var l;
	var map;
    var value;
    var hasOwnProperty = {}.hasOwnProperty;
    var obj = {
      'a': 1,
      'b': 2,
      'c': 3,
      'd': 4,
      'e': 5,
      'f': 6,
      'g': 7,
      'h': 8,
      'i': 9,
      'j': 10
    };
	var fakeMap = Object.create(null);
	for(let key of Object.keys(obj)) {
		fakeMap[key] = obj[key];
	}
    var prototype = Object.getPrototypeOf(obj);
    keys = Object.keys(obj);
    map = new Map(Object.entries(obj))
    keySet = new Set(keys);
    

Test runner

Ready to run.

Testing in
TestOps/sec
hasOwnProperty
if (obj.hasOwnProperty('j')) {
	obj.hasOwnProperty('k');
}
ready
Check prototype
if(!'j' in prototype && 'j' in obj) {
    !'k' in prototype && 'k' in obj
}
ready
cached indexOf
if(keys.indexOf('j') >= 0) {
	keys.indexOf('k');
}
ready
cached Set
if (keySet.has('j')) {
	keySet.has('k');
}
ready
map
if (map.has('j')) {
	map.has('k');
}
ready
dangerous
if('j' in obj) {
   'k' in obj;
}
ready
fakeMap
if('j' in fakeMap) {
   'k' in fakeMap;
}
ready
fakeMap again
if('j' in fakeMap) {
   'k' in fakeMap;
}
ready

Revisions

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