Loop or clone (v2)

Revision 2 of this benchmark created on


Setup

let i;
let entitiesArray = [];
let keysToDelete = [];
let entities;

for (i = 0; i < 10000; i++) {
  if(i <= 1000) {
    keysToDelete[i] = i;
  }
    entitiesArray[i] = Math.random();
}

entities = Object.assign({}, entitiesArray)

Teardown

i = 0;
entities = [];
keysToDelete = [];
entities = {};

Test runner

Ready to run.

Testing in
TestOps/sec
Filter foreach

const newEntities = {};

Object.keys(entities)
	.filter(key => !keysToDelete.includes(key))
	.forEach(key => (newEntities[key] = entities[key]));
ready
clone and delete operator
const newEntities = structuredClone(entities);

  keysToDelete.forEach(key => {
    delete newEntities[key];
  });
ready
for in loop
const newEntities = {};
 
  for (const key in entities) {
    if (!keysToDelete.includes(key)) {
      newEntities[key] = entities[key];
    }
  }
ready
for loop
const newEntities = {};
const entityKeys = Object.keys(entities);

  for (let i = 0; i < entities.length; i++) {
    const key = entityKeys[i];
    if (!keysToDelete.includes(key)) {
      newEntities[key] = entities[key];
    }
  }
ready

Revisions

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