const obj = {
hello: "world"
};
if (Math.random()>.5) {
obj.foo = "bar";
}
const obj = {
hello: "world",
Math.random()>.5 && { foo: "bar" }
};
const obj = Object.assign({
hello: "world"
}, Math.random()>.5 && {
foo: "bar"
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
boring | |
spread | |
Object.assign |
Test name | Executions per second |
---|---|
boring | 34740276.0 Ops/sec |
spread | 6666045.0 Ops/sec |
Object.assign | 8506570.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The provided benchmark measures the performance of three different approaches to conditionally insert properties into an object:
Object.assign()
method used with an initial object and a conditional assignment.Options Compared
These three approaches are compared in terms of their performance, which is measured by the number of executions per second on a specific device (Firefox Mobile 88, Android 11).
Pros and Cons of Each Approach
Library/Functionality Used
In this benchmark, there are no specific libraries used beyond the built-in JavaScript functions and methods mentioned above. However, it's worth noting that some browsers may have additional features or optimizations enabled in their implementations of these functions (e.g., Firefox has a custom implementation of Object.assign()
).
Special JS Features/Syntax
None of the benchmark cases use special JavaScript features or syntax beyond what's covered by the standard language specification.
Other Alternatives
If you're looking for alternatives to these approaches, consider:
For more information on measuring JavaScript performance or creating microbenchmarks, MeasureThat.net offers various resources and guides to help you get started.