var numKey1 = 794456;
var numKey2 = 2016;
var strKey1 = '794456';
var strKey2 = '2016'
var out;
out = JSON.stringify([ numKey1, numKey2 ]);
out = JSON.stringify([ strKey1, strKey2 ]);
out = `[${ numKey1 },${ numKey2 }]`;
out = `[${ strKey1 },${ strKey2 }]`;
out = '[' + numKey1 + ',' + numKey2 + ']';
out = '[' + strKey1 + ',' + strKey2 + ']';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON.stringify (numbers) | |
JSON.stringify (strings) | |
Template Strings (numbers) | |
Template Strings (strings) | |
String Concatenation (numbers) | |
String Concatenation (strings) |
Test name | Executions per second |
---|---|
JSON.stringify (numbers) | 842754.3 Ops/sec |
JSON.stringify (strings) | 701421.1 Ops/sec |
Template Strings (numbers) | 1610547.2 Ops/sec |
Template Strings (strings) | 1629762.5 Ops/sec |
String Concatenation (numbers) | 1645775.8 Ops/sec |
String Concatenation (strings) | 1720763.4 Ops/sec |
Let's dive into the explanation.
Benchmark Overview
The benchmark measures the performance of different ways to generate complex keys in JavaScript, specifically for use in a Map
lookup. The test cases focus on comparing four approaches: string concatenation, template strings, JSON.stringify()
, and an alternative syntax (not mentioned in the provided benchmark).
Approaches Compared
+
operator to concatenate strings or numbers.JSON.stringify()
function to convert an array or object to a JSON string.Library Used
None mentioned in the provided benchmark, but JSON.stringify()
relies on built-in JavaScript functionality and doesn't require any external libraries.
Special JS Features/Syntax
JSON.stringify()
has been available since Internet Explorer 9 (IE9), but its usage is generally considered more modern.Other Alternatives
For generating complex keys, other approaches might include:
Map
.map()
or reduce()
to generate keys.%
operator).Please note that these alternatives might not be as well-supported or widely tested as the approaches included in this benchmark.
I hope this explanation helps you understand the provided benchmark!