Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string vs numeric enum values compare
(version: 1)
Comparing performance of:
string enums vs numeric enums
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
string enums
const a = { one: 'one', two: 'two', }; let c = 0; for (let i = 0; i < 100000; i++) { const v = i % 2 === 0 ? a.one : a.two; if (v === a.one) { c++; } }
numeric enums
const a = { one: 1, two: 2, }; let c = 0; for (let i = 0; i < 100000; i++) { const v = i % 2 === 0 ? a.one : a.two; if (v === a.one) { c++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
string enums
numeric enums
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
string enums
3838.7 Ops/sec
numeric enums
4973.7 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
### Benchmark Overview This benchmark tests the performance of two approaches for defining and comparing enumerated values in JavaScript: one using string literals and the other using numeric literals. The primary goal is to analyze how these two strategies affect the performance of equal comparisons in a loop. ### Test Cases 1. **String Enums**: ```javascript const a = { one: 'one', two: 'two', }; let c = 0; for (let i = 0; i < 100000; i++) { const v = i % 2 === 0 ? a.one : a.two; if (v === a.one) { c++; } } ``` - **Test Name**: string enums - **Explanation**: This test iterates 100,000 times, selecting between two string values, `a.one` ('one') and `a.two` ('two'), based on the index's parity (even or odd). It increments the counter `c` each time the selected value is equal to `a.one`. 2. **Numeric Enums**: ```javascript const a = { one: 1, two: 2, }; let c = 0; for (let i = 0; i < 100000; i++) { const v = i % 2 === 0 ? a.one : a.two; if (v === a.one) { c++; } } ``` - **Test Name**: numeric enums - **Explanation**: Similar to the string test, this version uses numeric values instead. The constant values `1` and `2` are used instead of strings, and comparisons are made in the same manner. ### Performance Results The benchmark results show the following executions per second for each test: - **Numeric Enums**: 5267.26 executions per second - **String Enums**: 4141.78 executions per second ### Pros and Cons #### String Enums **Pros**: - More expressive and descriptive, making code easier to read and understand. - Works well for scenarios requiring descriptive naming, such as in many business logic applications. **Cons**: - Generally slower during comparisons due to the overhead of string comparison (which involves checking multiple characters). - Can consume more memory due to the longer nature of strings compared to simple numbers. #### Numeric Enums **Pros**: - Typically faster for comparison operations since numeric comparison is usually less expensive than string comparison. - More memory-efficient as they occupy less space compared to strings. **Cons**: - Less descriptive; using numbers can reduce code readability, especially if not properly documented. - May not provide meaningful context without additional mapping or documentation. ### Other Considerations When deciding between string and numeric enums, consider the specific use case: - If performance is a critical aspect (e.g., in a performance-intensive loop), numeric enums might be preferable. - If readability and maintainability are equally important, string enums may offer a better balance. ### Alternatives Besides using string and numeric enums, developers can consider other options: 1. **Symbol Enums**: Using JavaScript's `Symbol` provides unique identifiers, which are helpful in avoiding name conflicts and also being fast in comparisons. 2. **Object Keys**: Using objects or maps to represent a collection of related constants, though this can introduce additional overhead. 3. **TypeScript Enums**: If type safety and clearer structure are required, TypeScript offers advanced enum capabilities that combine some benefits of string and numeric approaches. Ultimately, the choice between these options will depend on the specific requirements of the application, including performance constraints, code maintainability, and team preferences.
Related benchmarks:
sdfasfasdfabc
more vs not equal
Comparing int to string using loose equality
Test shitty shit
String Comparison vs Int Comparison
Comparison Check (number VS string)
Strict equality performance (operands of different types)
array.indexof vs for
Which equals operator (== vs ===) is faster (different types)?
Comments
Confirm delete:
Do you really want to delete benchmark?