Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Convert boolean to number
(version: 1)
May useful for some bit calculation that require high performance.
Comparing performance of:
+ vs Number call vs ? : vs if vs Number
Created:
6 months ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const arr = Array.from({ length: 100000 }, () => Math.random() > 0.5);
Tests:
+
const converted = arr.map(v => +v);
Number call
const converted = arr.map(v => Number(v));
? :
const converted = arr.map(v => v ? 1 : 0);
if
const converted = arr.map(v => { if (v) return 1; else return 0; });
Number
const converted = arr.map(Number);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
+
Number call
? :
if
Number
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
+
1423.5 Ops/sec
Number call
377.2 Ops/sec
? :
963.7 Ops/sec
if
962.4 Ops/sec
Number
520.5 Ops/sec
Related benchmarks:
Get max from an array of numbers (Math.max vs. iteration) V3
Ranged array mapping
Array cloning 2023 (2)
Compare the ways to convert String to Number in JS
Reduce vs Filter and Map
int to string convertation
Set/Map symbol vs Obj vs String vs Number
Compare the ways to convert String to Number in JS +
abs vs ** 2 vs math.pow
Comments
Confirm delete:
Do you really want to delete benchmark?