React Prop Removal

JavaScript performance comparison

Test case created

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
      };
    }
};
</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
Two Loops (with Delete)
var instance = createInstance({type: 'text', className: ''});

var lastProps = instance.lastProps;
var nextProps = instance.nextProps;

for (propKey in lastProps) {
  if (lastProps.hasOwnProperty(propKey) &&
     !nextProps.hasOwnProperty(propKey)) {
    instance.deleteProp(propKey);
  }
}
for (propKey in nextProps) {
  if (nextProps.hasOwnProperty(propKey)) {
    if (lastProps[propKey] !== nextProps[propKey]) {
      instance.updateProp(propKey, nextProps[propKey]);
    }
  }
}
pending…
Two Loops (without Delete)
var instance = createInstance({type: 'text', className: '', value: '?'});

var lastProps = instance.lastProps;
var nextProps = instance.nextProps;

for (propKey in lastProps) {
  if (lastProps.hasOwnProperty(propKey) &&
     !nextProps.hasOwnProperty(propKey)) {
    instance.deleteProp(propKey);
  }
}
for (propKey in nextProps) {
  if (nextProps.hasOwnProperty(propKey)) {
    if (lastProps[propKey] !== nextProps[propKey]) {
      instance.updateProp(propKey, nextProps[propKey]);
    }
  }
}
pending…
One Loop (with Delete)
var instance = createInstance({type: 'text', className: ''});

var lastProps = instance.lastProps;
var nextProps = instance.nextProps;

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

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

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

lastCount = nextCount;
pending…
One Loop (without Delete)
var instance = createInstance({type: 'text', className: '', value: '?'});

var lastProps = instance.lastProps;
var nextProps = instance.nextProps;

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

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

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

lastCount = nextCount;
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