Created
December 9, 2015 21:39
-
-
Save allenluce/d3a1dc59593b50bc3b70 to your computer and use it in GitHub Desktop.
Benchmarking hasOwnProperty vs. conditional test for property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
var suite = new Benchmark.Suite; | |
var req = { | |
someProperty: "some value" | |
}; | |
var property = "someProperty"; | |
var notAProperty = "someProperty"; | |
suite.add('with conditional test', function() { | |
if (!req[property]) | |
return false; | |
}).add('with hasOwnProperty', function() { | |
if (!req.hasOwnProperty(property)) | |
return false; | |
}).add('negative with conditional test', function() { | |
if (!req[notAProperty]) | |
return false; | |
}).add('negative with hasOwnProperty', function() { | |
if (!req.hasOwnProperty(notAProperty)) | |
return false; | |
}).on('cycle', function(event) { | |
console.log(String(event.target)); | |
}).on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}).run({ 'async': true }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with conditional test x 62,329,924 ops/sec ±0.10% (97 runs sampled) | |
with hasOwnProperty x 13,004,569 ops/sec ±0.35% (98 runs sampled) | |
negative with conditional test x 53,166,225 ops/sec ±0.12% (90 runs sampled) | |
negative with hasOwnProperty x 12,297,098 ops/sec ±0.10% (95 runs sampled) | |
Fastest is with conditional test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment