<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
var person = { name: 'Foo', lastName: 'Bar' };
_.set(person, 'age', 10)
person.age = 10
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Dot notation property setter | |
Lodash.set |
Test name | Executions per second |
---|---|
Dot notation property setter | 20882676.0 Ops/sec |
Lodash.set | 137567200.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The provided JSON represents a benchmark that compares two different approaches to set an object property:
person.age = 10
._.set()
function from the Lodash library, which is a popular utility library for JavaScript.What options are compared?
Two test cases are being run:
_.set(person, 'age', 10)
(using Lodash.set)person.age = 10
(using dot notation property setter)These two approaches are essentially setting the value of the age
property on an object called person
.
Pros and Cons:
Library: Lodash
The Lodash library is a popular utility library for JavaScript that provides a set of functional programming helpers and iterators. In this case, the _.set()
function is used to set an object property in a more robust way.
Special JS feature or syntax: None
No special JavaScript features or syntax are being used in this benchmark. The code is straightforward and follows standard JavaScript conventions.
Other alternatives:
There are other ways to set object properties in JavaScript, such as:
person['age'] = 10
{ age: 10 }
(in an object literal)Object.assign(person, { age: 10 })
These alternatives are not compared in this benchmark, but they can be used in other contexts to achieve similar results.
Overall, this benchmark provides a simple and effective way to compare different approaches to setting object properties in JavaScript.