objectsEqual

JavaScript performance comparison

Revision 4 of this test case created by Christian Engel

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const objectsEqualA = (objA, objB) => {
  	const objAKeys = Object.keys(objA);
  	const objBKeys = Object.keys(objB);
  
  	const valueIsEqual = key => objB.hasOwnProperty(key) && objA[key] === objB[key];
  
  	return (
  		objAKeys.length === objBKeys.length
  		&& objectAKeys.every(valueIsEqual)
  	);
  };
  
  const objectsEqualB = (objA, objB) => {
      const objAKeys = Object.keys(objA).sort();
      const objBKeys = Object.keys(objB).sort();
  
      if (objAKeys.length !== objBKeys.length) {
          return false;
      }
  
      for (let i = 0; i < objAKeys.length; i++) {
          if (objAKeys[i] !== objBKeys[i]) {
              return false;
          }
          const key = objAKeys[i];
  
          if (objA[key] !== objB[key]) {
              return false;
          }
      }
      return true;
  };
  
  const objectsEqualC = (objA, objB) => {
  	const objAKeys = Object.keys(objA);
  	const objBKeys = Object.keys(objB);
  
  	if(objAKeys.length !== objBKeys.length){
  		return false;
  	}
  
  	for(let i = 0; i < objectAKeys.length; i++){
  		const key = objectAKeys[i];
  		if(!objB.hasOwnProperty(key)){
  			return false;
  		}
  		if(objA[key] !== objB[key]){
  			return false;
  		}
  	}
  	return true;
  }
  const o1 = {ameise: 'hallo', banane: 'welt', 'chameleon-drama': 'bla'};
  const o2 = {nest: 'vogel', was: 'auch immer'};

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Old method
objectsEqualB(o1, o2);
pending…
Simplified
objectsEqualA(o1, o2);
pending…
Simplified, no function calls
objectsEqualC(o1, o2);
pending…

Compare results of other browsers

Revisions

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

0 Comments