NonEmpty dependent object

Benchmark created by Brad Momberger on


Setup

window.manythingsdict = {
  foo : [],
  bar : [],
  baz : [],
  quux : [],
  thud : [],
  jeek : [],
  plonk : [],
  waldo : [],
  xyzzy : [],
  xyzzzy : [],
  xyzzzzy : ['xyzzzzzy']
  };
  Object.setPrototypeOf(manythingsdict, null);
  
    window.manythingsobj = {
    foo : [],
    bar : [],
    baz : [],
    quux : [],
    thud : [],
    jeek : [],
    plonk : [],
    waldo : [],
    xyzzy : [],
    xyzzzy : [],
    xyzzzzy : ['xyzzzzzy']
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Lots of items, no setprototype, instanceof check
var object = { _objects: manythingsobj };

var result = false;
var i;
for(var i in object._objects) {
  if (object._objects[i] instanceof Array && object._objects[i].length)
     result = true; break;
}
ready
Numeric property (baseline)
var object = { _count: 1, _objects: { foo: [] } };
var result;

if(object._count) {
  result = true;
} else {
  result = false;
}
ready
Object.keys with delete
var object = { _objects: { foo: [] } };

var result;
delete object._objects.foo;
if(Object.keys(object._objects).length < 1) {
  result = true;
} else {
  result = false;
}
ready
with loop and Object.create
var dict = Object.create(null);
dict.foo = [];
var object = { _objects: dict };

var result = false;
var i;
for(var i in object._objects) {
  result = true; break;
}
ready
with loop
var object = { _objects: { foo: [] } };

var result = false;
var i;
for(var i in object._objects) {
  result = true; break;
}
ready
with faked object.create
var dict = {};
dict.__proto__ = null;
dict.foo = [];
var object = { _objects: dict };

var result = false;
var i;
for(var i in object._objects) {
  result = true; break;
}
ready
setPrototypeOf
var dict = {};
Object.setPrototypeOf(dict, null);
dict.foo = ['bar'];
var object = { _objects: dict };

var result = false;
var i;
for(var i in object._objects) {
  result = true; break;
}
ready
with length check
var object = { _objects: { foo: [] } };

var result = false;
var i;
for(var i in object._objects) {
  if (object._objects[i].constructor === Array && object._objects[i].length)
     result = true; break;
}
ready
with length check non empty
var object = { _objects: { foo: ['bar'] } };

var result = false;
var i;
for(var i in object._objects) {
  if (object._objects[i].constructor === Array && object._objects[i].length)
     result = true; break;
}
ready
length check & setprototypeof
var dict = {};
Object.setPrototypeOf(dict, null);
dict.foo = ['bar'];
var object = { _objects: dict };

var result = false;
var i;
for(var i in object._objects) {
  if (object._objects[i].length)
     result = true; break;
}
ready
Lots of items
var dict = {};
Object.setPrototypeOf(dict, manythingsdict);
var object = { _objects: dict };

var result = false;
var i;
for(var i in object._objects) {
  if (object._objects[i].length)
     result = true; break;
}
ready
Delete item, then empty object test
var dict = {};
Object.setPrototypeOf(dict, null);
dict.foo = ['bar'];
var object = { _objects: dict };

var result = false;
var i;
delete object._objects.foo;
for(var i in object._objects) {
  if (object._objects[i].length)
     result = true; break;
}
ready
Lots of items, no setprototype
var object = { _objects: manythingsobj };

var result = false;
var i;
for(var i in object._objects) {
  if (object._objects[i].constructor === Array && object._objects[i].length)
     result = true; break;
}
ready
Lots of items, no setprototype, own prop check
var object = { _objects: manythingsobj };

var result = false;
var i;
for(var i in object._objects) {
  if (object._objects.hasOwnProperty(i) && object._objects[i].length)
     result = true; break;
}
ready

Revisions

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

  • Revision 1: published by Brad Momberger on