Skip to content

Instantly share code, notes, and snippets.

@acao
Created October 28, 2017 19:09
Show Gist options
  • Save acao/06fd3951c8e9174777d497a9f3fe77ed to your computer and use it in GitHub Desktop.
Save acao/06fd3951c8e9174777d497a9f3fe77ed to your computer and use it in GitHub Desktop.
Get/Set Benchmark Step 3
<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