const myVar = 'test'
const mySet = new Set();
const myMap = new Map();
const myObj = {};
for(let i = 0; i < 100; i++) {
const newVar = `${myVar}${i}`;
mySet.add(newVar);
myMap.set(newVar, 0);
myObj[newVar] = 0;
}
const length = 100000000;
for (let i=0; i < length; i++) {
myObj.test = 1;
}
for (let i=0; i < length; i++) {
if (!myObj.test) myObj.test = 1;
}
for (let i=0; i < length; i++) {
mySet.add('test');
}
for (let i=0; i < length; i++) {
if (!mySet.has('test')) mySet.add('test');
}
for (let i=0; i < length; i++) {
myMap.set('test', 1);
}
for (let i=0; i < length; i++) {
if (!myMap.get('test')) myMap.set('test', 1);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object Assignment | |
Object Assignment + condition | |
Set assignment | |
Set assignment + condition | |
Map assignment | |
Map assignment + condition |
Test name | Executions per second |
---|---|
Object Assignment | 10185250.0 Ops/sec |
Object Assignment + condition | 10208501.0 Ops/sec |
Set assignment | 10278274.0 Ops/sec |
Set assignment + condition | 10246101.0 Ops/sec |
Map assignment | 10304539.0 Ops/sec |
Map assignment + condition | 10249774.0 Ops/sec |
Let's dive into the benchmark.
Benchmark Overview
The test measures the performance of different ways to assign values to variables, maps, sets, and objects in JavaScript. The goal is to understand which approach is fastest across various browsers and platforms.
Script Preparation Code
Before we analyze each test case, let's review the script preparation code:
const myVar = 'test';
const mySet = new Set();
const myMap = new Map();
const myObj = {};
This code initializes four variables: myVar
, mySet
, myMap
, and myObj
. It also sets up a loop that will be used in the test cases.
Test Cases
Each test case is defined by a "Benchmark Definition" string, which contains JavaScript code that performs an assignment operation. The tests are:
for (let i=0; i < length; i++) { myObj.test = 1; }
for (let i=0; i < length; i++) { if (!myObj.test) myObj.test = 1; }
for (let i=0; i < length; i++) { mySet.add('test'); }
for (let i=0; i < length; i++) { if (!mySet.has('test')) mySet.add('test'); }
for (let i=0; i < length; i++) { myMap.set('test', 1); }
for (let i=0; i < length; i++) { if (!myMap.get('test')) myMap.set('test', 1); }
Options Compared
The test cases compare the performance of different assignment approaches:
myObj.test = 1
)if (!myObj.test) myObj.test = 1
)For sets and maps, the tests compare:
mySet.add('test')
or myMap.set('test', 1)
)if (!mySet.has('test')) mySet.add('test')
or if (!myMap.get('test')) myMap.set('test', 1)
)Pros and Cons
Here are some pros and cons of each approach:
null
or undefined
values).false
.For sets and maps, the simple assignment approach is generally faster because it avoids the overhead of checking for existence. However, the conditionals ensure that the values are set correctly even in edge cases.
Library and Special JS Features
None of the test cases use any libraries or special JavaScript features beyond standard JavaScript syntax.
Other Alternatives
If you want to improve performance or handle specific edge cases, consider using:
myObj?.test = 1;
): These can help avoid unnecessary computations.However, these optimizations may come at the cost of increased code complexity or slower performance for certain workloads.
Keep in mind that the best approach depends on your specific use case and performance requirements.