resolve vs fallback

JavaScript performance comparison

Test case created by kurkle

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var input = [undefined, undefined, 42];
  var context = undefined;
  var index = undefined;
  function resolvev(context, index) {
      var hasContext = context !== undefined;
      var hasIndex = index !== undefined;
  		var i, ilen, value;
  
  		for (i = 2, ilen = arguments.length; i < ilen; ++i) {
  			value = arguments[i];
  			if (value === undefined) {
  				continue;
  			}
  			if (hasContext  && typeof value === 'function') {
  				value = value(context);
  			}
  			if (hasIndex  && helpers.isArray(value)) {
  				value = value[index];
  			}
  			if (value !== undefined) {
  				return value;
  			}
  		}
  	}
  
  function resolvec(inputs, context, index) {
      var hasContext = context !== undefined;
      var hasIndex = index !== undefined;
  		var i, ilen, value;
  
  		for (i = 0, ilen = inputs.length; i < ilen; ++i) {
  			value = inputs[i];
  			if (value === undefined) {
  				continue;
  			}
  			if (hasContext  && typeof value === 'function') {
  				value = value(context);
  			}
  			if (hasIndex  && helpers.isArray(value)) {
  				value = value[index];
  			}
  			if (value !== undefined) {
  				return value;
  			}
  		}
  	}
   function resolve(inputs, context, index) {
  		var i, ilen, value;
  
  		for (i = 0, ilen = inputs.length; i < ilen; ++i) {
  			value = inputs[i];
  			if (value === undefined) {
  				continue;
  			}
  			if (context !== undefined && typeof value === 'function') {
  				value = value(context);
  			}
  			if (index !== undefined && helpers.isArray(value)) {
  				value = value[index];
  			}
  			if (value !== undefined) {
  				return value;
  			}
  		}
  	}
  	function _fallback() {
  		var i, ilen, value;
  
  		for (i = 0, ilen = arguments.length - 1; i < ilen; ++i) {
  			value = arguments[i];
  			if (typeof value !== 'undefined') {
  				return value;
  			}
  		}
  		return arguments[i];
  	}
  	function _fallback2() {
  		var i, ilen, value;
  
  		for (i = 0, ilen = arguments.length - 1; i < ilen; ++i) {
  			value = arguments[i];
  			if (value !== undefined) {
  				return value;
  			}
  		}
  		return arguments[i];
  	}
  function wrap() {
   return resolve(arguments);
  }

};
</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
resolve
resolve([undefined,undefined,42]);
pending…
fallback
_fallback(undefined,undefined,42);
pending…
resolvec
resolvec([undefined,undefined,42]);
pending…
fallback2
_fallback2(undefined,undefined,42);
pending…
resolve_pre
resolve(input);
pending…
resolvec_pre
resolvec(input)
pending…
resolvev
resolvev(context, index, undefined, undefined, 42);
pending…

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

Compare results of other browsers

0 Comments