var foo1 = {};
var foo2 = {};
var sym = [Symbol("key1"), Symbol("key2"), Symbol("key3"), Symbol("key4"), Symbol("key5"), Symbol("key6"), Symbol("key7"), Symbol("key8"), Symbol("key9"), Symbol("key0")];
var str = ["key1", "key2", "key3", "key4", "key5", "key6", "key7", "key8", "key9", "key0"];
for(const k of sym) foo1[k] = 1;
for(const k of str) foo2[k] = 1;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Symbol | |
String |
Test name | Executions per second |
---|---|
Symbol | 2786229.0 Ops/sec |
String | 2723901.2 Ops/sec |
Let's dive into the benchmark definition and test cases.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark, which is a small piece of code designed to measure the performance of specific aspects of JavaScript. In this case, the benchmark measures the performance difference between using Symbol properties versus String properties in an array loop.
Script Preparation Code
Before running the tests, the script preparation code defines two objects: foo1
and foo2
, both initialized as empty objects. Additionally, it defines a sym
variable, which is an array of 10 Symbols with unique keys (using the Symbol()
function), and a corresponding str
variable, which is an array of 10 strings.
Html Preparation Code
There is no HTML preparation code provided, indicating that this benchmark does not rely on any specific HTML structure or rendering.
Individual Test Cases
The benchmark consists of two test cases:
sym
array and assigns a value to each Symbol property using a for...of
loop.str
array and assigns a value to each string index using a similar for...of
loop.Options Compared
The benchmark compares two options:
sym
) as property keysstr
) as property keysPros and Cons of Different Approaches
Using Symbols has several advantages over using strings as property keys:
Pros:
Cons:
On the other hand, using strings has its own advantages:
Pros:
Cons:
Library and Purpose
In this benchmark, no libraries are explicitly required. However, it's worth noting that some of these libraries may influence performance:
Special JS Features or Syntax
The benchmark uses the Symbol()
function, which is a built-in JavaScript feature introduced in ECMAScript 2015 (ES6). This allows for creating unique and unambiguous property keys.
However, since this benchmark primarily focuses on comparing Symbol and string properties, it does not rely on any other advanced JavaScript features or syntax.