Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Symbol vs String property square bracket, loop 2
(version: 0)
Comparing performance of:
Symbol vs String vs Symbol sum vs String sum
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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"]; var foo1 = { [sym[0]]: 0, [sym[1]]: 0, [sym[2]]: 0, [sym[3]]: 0, [sym[4]]: 0, [sym[5]]: 0, [sym[6]]: 0, [sym[7]]: 0, [sym[8]]: 0, [sym[9]]: 0, }; var foo2 = { [str[0]]: 0, [str[1]]: 0, [str[2]]: 0, [str[3]]: 0, [str[4]]: 0, [str[5]]: 0, [str[6]]: 0, [str[7]]: 0, [str[8]]: 0, [str[9]]: 0 };
Tests:
Symbol
for(const k of sym) foo1[k] = 1;
String
for(const k of str) foo2[k] = 1;
Symbol sum
let sum = 0 for (const k of sym) sum += foo1[k];
String sum
let sum = 0 for (const k of str) sum += foo2[k];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Symbol
String
Symbol sum
String sum
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Symbol
2816764.0 Ops/sec
String
2823161.2 Ops/sec
Symbol sum
2925757.2 Ops/sec
String sum
2959609.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an in-depth explanation of the provided benchmark. **Benchmark Overview** The benchmark measures the performance difference between using Symbol properties and string indices (also known as arrays) for object access in JavaScript. The benchmark consists of four test cases: 1. `Symbol`: Accessing elements using Symbol properties. 2. `String`: Accessing elements using string indices (arrays). 3. `Symbol sum`: Calculating the sum of element values by iterating over Symbol-located elements. 4. `String sum`: Calculating the sum of element values by iterating over array-located elements. **Library and Special JS Features** There is no explicit library mentioned in the benchmark, but JavaScript's built-in `Symbol` feature is used to create unique property names. The `Symbol` feature was introduced in ECMAScript 2015 (ES6) as a way to create new property names that cannot be accessed by string indices. **Options Compared** The benchmark compares two options: 1. **Symbol properties**: Using Symbol properties for object access, where each property has a unique name that can only be accessed using its corresponding Symbol value. 2. **String indices (arrays)**: Using array indices to access elements of an object, where the index is used as both a key and a value. **Pros and Cons** **Symbol Properties:** Pros: * Unique property names that cannot be accessed by string indices * Can improve performance due to reduced collisions with existing property names Cons: * Less intuitive for developers who are familiar with string indices * Requires explicit creation of Symbol values using `Symbol()` **String Indices (Arrays):** Pros: * More intuitive and easier to understand, especially for developers familiar with array indexing * Does not require explicit creation of unique property names Cons: * Can result in collisions with existing property names * May lead to slower performance due to increased lookup times **Benchmark Results** The latest benchmark results show the following execution rates per second: * `String sum`: 2959609.75 executions per second (Chromium 119) * `Symbol sum`: 2925757.25 executions per second (Chromium 119) * `String`: 2823161.25 executions per second (Chromium 119) * `Symbol`: 2816764.0 executions per second (Chromium 119) These results suggest that accessing elements using Symbol properties is slightly faster than using string indices. **Other Alternatives** While the benchmark focuses on Symbol properties and string indices, other alternatives for object access include: 1. **Object Literals**: Using named properties (strings) to access elements of an object. 2. **Numeric Indices**: Accessing elements using numeric values as both keys and values. 3. **Computed Properties**: Using computed property names (e.g., `obj[Symbol('name')]`) for dynamic or runtime-determined property names. Keep in mind that the performance benefits of each approach depend on specific use cases, code organization, and the trade-offs between developer intuition, maintainability, and performance optimization.
Related benchmarks:
Symbol vs String property square bracket fork
Symbol vs String property square bracket a
Symbol vs String property square bracket increment
Symbol vs String property square bracket, loop
Comments
Confirm delete:
Do you really want to delete benchmark?