React Prop Removal

JavaScript performance comparison

Revision 4 of this test case created by jordow

Info

Removes hasOwnProperty which dilutes the results and makes sure that internal objects are accessed in a consistent order across tests.

Skips creation of new objects in test case (isolating diff)

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function createInstance(nextProps) {
      var lastProps = {type: 'text', className: 'hidden_elem', value: 'hello'};
   
      var lastCount = 3;
      var propKey;
   
      var mounted = {type: 'text', className: 'hidden_elem', value: 'hello'};
   
      function updateProp(name, value) {
        mounted[name] = value;
      }
   
      function deleteProp(name) {
        mounted[name] = undefined;
      }
   
      return {
        lastProps: lastProps,
        nextProps: nextProps,
        lastCount: lastCount,
        updateProp: updateProp,
        deleteProp: deleteProp
      };
    }
   
    var instance = createInstance({type: 'text', className: 'hidden_elem', value: 'hello'});
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Prop Counting (without change)
var lastProps = instance.lastProps;
var nextProps = instance.nextProps;

var lastCount = instance.lastCount;
var missCount = lastCount;
var nextCount = 0;

for (propKey in nextProps) {
    nextCount++;
    if (lastProps[propKey] || (propKey in lastProps)) {
      missCount--;
    }
    if (lastProps[propKey] !== nextProps[propKey]) {
      instance.updateProp(propKey, nextProps[propKey]);
    }
}

if (missCount > 0) {
  for (propKey in lastProps) {
    if (!(propKey in nextProps)) {
      missCount--;
      instance.deleteProp(propKey);
      if (!missCount) {
        break;
      }
    }
  }
}

if(nextCount !== lastCount) {
   instance.lastCount = nextCount;
}
pending…
Two Loops (without Change)
var lastProps = instance.lastProps;
var nextProps = instance.nextProps;

for (propKey in lastProps) {
  if (!nextProps[propKey] || !(propKey in nextProps)) {
    instance.deleteProp(propKey);
  }
}
for (propKey in nextProps) {
  if (lastProps[propKey] !== nextProps[propKey]) {
    instance.updateProp(propKey, nextProps[propKey]);
  }
}
pending…
One Loop (Legacy, Incorrect)
var lastProps = instance.lastProps;
var nextProps = instance.nextProps;

for (propKey in nextProps) {
    if (lastProps[propKey] !== nextProps[propKey]) {
      instance.updateProp(propKey, nextProps[propKey]);
    }
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment