Created
October 28, 2017 19:09
-
-
Save acao/06fd3951c8e9174777d497a9f3fe77ed to your computer and use it in GitHub Desktop.
Get/Set Benchmark Step 3
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
<script> | |
var suite = new Benchmark.Suite(); | |
const obj = { | |
stuff: { | |
things: { | |
stuff: 'things', | |
things: 'stuff', | |
other: { | |
stuff: 'things' | |
} | |
} | |
} | |
}; | |
const path = 'stuff.things.other.stuff'; | |
const valToSet = 9999; | |
suite | |
.add('lodash get', () => { | |
_.get(obj, path); | |
_.set(obj, path, valToSet); | |
}) | |
.add('object-path get', () => { | |
objectPath.get(obj, path); | |
objectPath.set(obj, path, valToSet); | |
}) | |
.add('vanilla get', () => { | |
const pathExists = | |
obj.stuff && | |
obj.stuff.things && | |
obj.stuff.things.other && | |
obj.stuff.things.other.hasOwnProperty('stuff') && | |
obj.stuff.things.other.stuff; | |
if (pathExists) { | |
obj[path] = valToSet; | |
} | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target.fn)); | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
// run async | |
.run(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment