Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
comparing string vs numbers vs bits
(version: 0)
Comparing performance of:
Values as strings vs Values as numbers vs Values as binary
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Values as strings
const random = { reconnect: "reconnect", activate: "activate", translate: "translate", document: "document", analyze: "analyze", transform: "transform", calculate: "calculate", develop: "develop", optimize: "optimize", integrate: "integrate", visualize: "visualize", compile: "compile", maintain: "maintain", generate: "generate", collaborate: "collaborate", synchronize: "synchronize", evaluate: "evaluate", innovate: "innovate", communicate: "communicate", implement: "implement" }; function lookForRandom(key) { for(let k in random) { if (k === key) { console.log(1); } } } lookForRandom("implement");
Values as numbers
const random = { reconnect: 1, activate: 2, translate: 3, document: 4, analyze: 5, transform: 6, calculate: 7, develop: 8, optimize: 9, integrate: 10, visualize: 11, compile: 12, maintain: 13, generate: 14, collaborate: 15, synchronize: 16, evaluate: 17, innovate: 18, communicate: 19, implement: 20 }; function lookForRandom(key) { for(let k in random) { if (k === key) { console.log(1); } } } lookForRandom(20);
Values as binary
const random = { reconnect: 1<<1, activate: 1<<2, translate: 1<<3, document: 1<<4, analyze: 1<<5, transform: 1<<6, calculate: 1<<7, develop: 1<<8, optimize: 1<<9, integrate: 10<<1, visualize: 11<<2, compile: 12<<3, maintain: 13<<4, generate: 14<<5, collaborate: 15<<6, synchronize: 16<<7, evaluate: 17<<8, innovate: 18<<9, communicate: 19<<10, implement: 20<<11 }; function lookForRandom(key) { for(let k in random) { if (k === key) { console.log(1); } } } lookForRandom(random.implement);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Values as strings
Values as numbers
Values as binary
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 128 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Values as strings
36978.5 Ops/sec
Values as numbers
4053114.8 Ops/sec
Values as binary
3342615.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark compares three approaches to store and retrieve values in a JavaScript object: 1. **Strings**: Values are stored as strings using double quotes (`"`) or single quotes (''). 2. **Numbers**: Values are stored as numbers (either integers or floating-point numbers). 3. **Binary**: Values are stored as binary literals using the `0b` prefix. **Options Compared** The benchmark measures the execution time of a loop that iterates over the values in the `random` object, checking if a specific key matches. The test cases vary only by the type of value used to store the key: * **Strings**: The key is stored as a string using double quotes (`"`) or single quotes (''). * **Numbers**: The key is stored as an integer number. * **Binary**: The key is stored as a binary literal using the `0b` prefix. **Pros and Cons of Each Approach** 1. **Strings**: Pros: * Easy to read and write. * Can be used for string manipulation. Cons: * Less efficient than numbers or binary literals, especially for large values. 2. **Numbers**: Pros: * Faster execution compared to strings or binary literals. * Suitable for numerical computations. Cons: * May lead to precision issues for floating-point numbers. 3. **Binary**: Pros: * Efficient storage and execution. * Suitable for cryptographic or bitwise operations. **Library Used** The benchmark uses a custom-built JavaScript object `random` that contains various keys with different values (strings, numbers, binary literals). The `lookForRandom` function iterates over the object to find a specific key. No external libraries are used. **Special JS Feature/Syntax** None of the test cases use special JavaScript features or syntax beyond the standard language. However, note that binary literals were introduced in ECMAScript 2015 (ES6), so this benchmark is using a relatively modern feature. **Other Alternatives** If you wanted to modify the benchmark to include alternative approaches, here are some ideas: * **Objects**: Store values as objects with keys and values instead of strings or numbers. * **Arrays**: Store values as arrays or array-like structures. * **Custom data types**: Use a custom data type, such as a struct-like object, to store values. * **Different storage mechanisms**: Compare the execution time of storing values in different data structures, like linked lists or trees. Feel free to modify the benchmark to explore these alternatives and see how they impact performance!
Related benchmarks:
floor vs trunc vs bit shift
Which equals operator (== vs ===) is faster (string vs int)?
Fastest results
String to number, parseInt, +, or * 1
Number.isInteger() vs typeof
Comments
Confirm delete:
Do you really want to delete benchmark?