Object.entries() iteration / allocation overhead (v3)

Revision 3 of this benchmark created on


Setup

const objectToSerialize = {
	father: {
	    name: "John",
    	age: 30
    },
    mother: {
    	name: "Maria",
    	age: 31
    },
    children: [
		{
		    name: "Sandra",
 	   		age: 6
		}, {
		    name: "Jake",
 	   		age: 4
		}
    ]
};

const constraints = {
    name: "Maria"
};

const constraintsEntries = Object.entries(constraints);
const constraintsMap = new Map(constraintsEntries);

Test runner

Ready to run.

Testing in
TestOps/sec
Object.entries() in loop
for (let i = 0; i < 1000; i ++) {
JSON.stringify(objectToSerialize, (key, value) => {
  let newValue = value;
  if (value && typeof value === 'object') {
	
  Object.entries(constraints).forEach(([excludedKey, excludedValue]) => {
    if (value[excludedKey] && value[excludedKey] === excludedValue) {
      newValue = {};
      
      Object.keys(value).forEach(topLevelKey => {
        newValue[topLevelKey] = 'foo';
      });

      newValue[excludedKey] = excludedValue;
    }
  });	
  
  }
  return newValue;
});	
}
ready
Object.entries() outside loop
for (let i = 0; i < 1000; i ++) {
JSON.stringify(objectToSerialize, (key, value) => {
  let newValue = value;
  if (value && typeof value === 'object') {

  constraintsEntries.forEach(([excludedKey, excludedValue]) => {
    if (value[excludedKey] && value[excludedKey] === excludedValue) {
      newValue = {};
      
      Object.keys(value).forEach(topLevelKey => {
        newValue[topLevelKey] = 'foo';
      });

      newValue[excludedKey] = excludedValue;
    }
  });		
  
  }
  return newValue;
});	
}
ready
Map outside loop
for (let i = 0; i < 1000; i ++) {
JSON.stringify(objectToSerialize, (key, value) => {
  let newValue = value;
  if (value && typeof value === 'object') {
 
  constraintsMap.forEach((excludedValue, excludedKey) => {
    if (value[excludedKey] && value[excludedKey] === excludedValue) {
      newValue = {};
      
      Object.keys(value).forEach(topLevelKey => {
        newValue[topLevelKey] = 'foo';
      });

      newValue[excludedKey] = excludedValue;
    }
  });	
  
  }
  return newValue;	
});	
}
ready
Allocation-free
for (let i = 0; i < 1000; i ++) {
JSON.stringify(objectToSerialize, (key, value) => {
  let newValue = value;
  if (value && typeof value === 'object') {
 
  constraintsMap.forEach((excludedValue, excludedKey) => {
    if (value[excludedKey] && value[excludedKey] === excludedValue) {
      newValue = {};
      
      for (const topLevelKey in value) {
      	if (value.hasOwnProperty(topLevelKey)) {
      	  newValue[topLevelKey] = 'foo';
      	}
      }

      newValue[excludedKey] = excludedValue;
    }
  });	
  
  }
  return newValue;	
});	
}
ready

Revisions

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