Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
enum check (string vs. number vs. symbol)
(version: 0)
Comparing performance of:
string vs number vs symbol
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var STRING_UNDEFINED = "undefined" var STRING_NULL = "null" var STRING_BOOLEAN = "boolean" var STRING_NUMBER = "number" var STRING_BIGINT = "bigint" var STRING_STRING = "string" var STRING_SYMBOL = "symbol" var STRING_OBJECT = "object" var STRING_FUNCTION = "function" var NUMBER_UNDEFINED = 0 var NUMBER_NULL = 1 var NUMBER_BOOLEAN = 2 var NUMBER_NUMBER = 3 var NUMBER_BIGINT = 4 var NUMBER_STRING = 5 var NUMBER_SYMBOL = 6 var NUMBER_OBJECT = 7 var NUMBER_FUNCTION = 8 var SYMBOL_UNDEFINED = Symbol.for("undefined") var SYMBOL_NULL = Symbol.for("null") var SYMBOL_BOOLEAN = Symbol.for("boolean") var SYMBOL_NUMBER = Symbol.for("number") var SYMBOL_BIGINT = Symbol.for("bigint") var SYMBOL_STRING = Symbol.for("string") var SYMBOL_SYMBOL = Symbol.for("symbol") var SYMBOL_OBJECT = Symbol.for("object") var SYMBOL_FUNCTION = Symbol.for("function") var stringValues = Array(1000).fill([STRING_FUNCTION, () => "Leggo!"]) var numberValues = Array(1000).fill([NUMBER_FUNCTION, () => "Leggo!"]) var symbolValues = Array(1000).fill([SYMBOL_FUNCTION, () => "Leggo!"])
Tests:
string
const stringTypes = [] for (const [type] of stringValues) { switch (type) { case STRING_UNDEFINED: stringTypes.push("undefined") break case STRING_NULL: stringTypes.push("null") break case STRING_BOOLEAN: stringTypes.push("boolean") break case STRING_NUMBER: stringTypes.push("number") break case STRING_BIGINT: stringTypes.push("bigint") break case STRING_STRING: stringTypes.push("string") break case STRING_SYMBOL: stringTypes.push("symbol") break case STRING_OBJECT: stringTypes.push("object") break case STRING_FUNCTION: stringTypes.push("function") break } }
number
const numberTypes = [] for (const [type] of numberValues) { switch (type) { case NUMBER_UNDEFINED: numberTypes.push("undefined") break case NUMBER_NULL: numberTypes.push("null") break case NUMBER_BOOLEAN: numberTypes.push("boolean") break case NUMBER_NUMBER: numberTypes.push("number") break case NUMBER_BIGINT: numberTypes.push("bigint") break case NUMBER_STRING: numberTypes.push("string") break case NUMBER_SYMBOL: numberTypes.push("symbol") break case NUMBER_OBJECT: numberTypes.push("object") break case NUMBER_FUNCTION: numberTypes.push("function") break } }
symbol
const symbolTypes = [] for (const [type] of symbolValues) { switch (type) { case SYMBOL_UNDEFINED: symbolTypes.push("undefined") break case SYMBOL_NULL: symbolTypes.push("null") break case SYMBOL_BOOLEAN: symbolTypes.push("boolean") break case SYMBOL_NUMBER: symbolTypes.push("number") break case SYMBOL_BIGINT: symbolTypes.push("bigint") break case SYMBOL_STRING: symbolTypes.push("string") break case SYMBOL_SYMBOL: symbolTypes.push("symbol") break case SYMBOL_OBJECT: symbolTypes.push("object") break case SYMBOL_FUNCTION: symbolTypes.push("function") break } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
string
number
symbol
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
string
103958.2 Ops/sec
number
224260.3 Ops/sec
symbol
210727.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **General Purpose** The test suite measures the performance of JavaScript switch statements when checking for different types (strings, numbers, and symbols) using various syntaxes. **String Type Test Case** In the "string" test case, a JavaScript switch statement is used to iterate through an array of string values (`stringValues`) containing function calls with string literals. The goal is to identify which type each value corresponds to by checking the `type` property returned by the function call. Here's the relevant benchmark definition: ```javascript for (const [type] of stringValues) { switch (type) { case STRING_UNDEFINED: stringTypes.push("undefined") break // ... other cases ... } } ``` **Number Type Test Case** Similarly, in the "number" test case, a JavaScript switch statement is used to iterate through an array of number values (`numberValues`) containing function calls with number literals. The goal is to identify which type each value corresponds to by checking the `type` property returned by the function call. Here's the relevant benchmark definition: ```javascript for (const [type] of numberValues) { switch (type) { case NUMBER_UNDEFINED: numberTypes.push("undefined") break // ... other cases ... } } ``` **Symbol Type Test Case** In the "symbol" test case, a JavaScript switch statement is used to iterate through an array of symbol values (`symbolValues`) containing function calls with symbols. The goal is to identify which type each value corresponds to by checking the `type` property returned by the function call. Here's the relevant benchmark definition: ```javascript for (const [type] of symbolValues) { switch (type) { case SYMBOL_UNDEFINED: symbolTypes.push("undefined") break // ... other cases ... } } ``` **Performance Comparison** The test measures the execution time of these switch statements for different types. The results indicate that the performance difference between string, number, and symbol types is relatively small. **Pros and Cons** * **Pros:** + Measures performance of switch statements with different types. + Helps identify any potential issues or bottlenecks in JavaScript switch statement implementation. * **Cons:** + May not be representative of real-world scenarios where type checking is crucial. + Limited scope, only testing specific use cases. **Interpretation** The benchmark results suggest that the performance differences between string, number, and symbol types are relatively small. However, there may be edge cases or specific implementation details that affect performance in certain situations. To further optimize JavaScript switch statements, additional tests could focus on: * Optimizing switch statement implementations for better performance. * Investigating the impact of type checking on performance in real-world scenarios. * Exploring alternative approaches to type checking (e.g., using `instanceof` or `typeof`) and their performance implications. Overall, this benchmark provides valuable insights into the performance characteristics of JavaScript switch statements with different types.
Related benchmarks:
For in vs Object.*.forEach
values
For in vs Object.*.forEach vs Object.keys
lodash each vs object.entries
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values v3
Comments
Confirm delete:
Do you really want to delete benchmark?