var map = new Map();
var weakMap = new WeakMap()
var array = [];
var object = {};
var symbol = Symbol('next');
map.get("0");
weakMap.get("0")
array[0]
object.a
object[symbol]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Map.get | |
WeakMap.get | |
array[0] | |
object.a | |
object[symbol] |
Test name | Executions per second |
---|---|
Map.get | 228652784.0 Ops/sec |
WeakMap.get | 131895488.0 Ops/sec |
array[0] | 220876960.0 Ops/sec |
object.a | 205974736.0 Ops/sec |
object[symbol] | 210589456.0 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark measures the performance of different data structures in JavaScript: Array, Object, Map, WeakMap, and using a Symbol. The goal is to compare how quickly each data structure can access a specific element or property.
Options Compared
Here are the options being compared:
Pros and Cons
Library and Special Features
The benchmark uses the following libraries and special features:
Benchmark Preparation Code
The preparation code sets up variables to create instances of each data structure:
var map = new Map();
var weakMap = new WeakMap();
var array = [];
var object = {};
This creates an empty Map, WeakMap, and Array, as well as an empty Object.
Individual Test Cases
Each test case measures the execution time for a specific operation:
map.get("0")
: Accessing an element in the Map using its index.weakMap.get("0")
: Accessing an element in the WeakMap using its index.array[0]
: Accessing the first element of the Array.object.a
: Accessing a property on the Object using its key name.object[symbol]
: Accessing a property on the Object using the Symbol as the key.Benchmark Results
The benchmark results show the execution time per second for each test case across multiple browser runs:
Other Alternatives
If you wanted to modify this benchmark or create a new one, consider the following alternatives: