Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String comparison functions, if vs array reduce & Math.sign
(version: 1)
Comparing performance of:
compareString() vs compareString2()
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const compareString = (a, b) => { if (a === null && b !== null) { return 1; } else if (a !== null && b === null) { return -1; } else if (a !== null && b !== null) { return a.toLowerCase() > b.toLowerCase() ? 1 : a.toLowerCase() === b.toLowerCase() ? 0 : -1; } else { return 0; } }; const compareString2 = (a, b) => { const s1 = a ? [...a.toLowerCase()].reduce((prev, char) => prev + char.charCodeAt(0), 0) : 0; const s2 = b ? [...b.toLowerCase()].reduce((prev, char) => prev + char.charCodeAt(0), 0) : 0; return a && b ? Math.sign(s1 - s2) : Math.sign(s2 - s1); };
Tests:
compareString()
compareString("Anita", "Ben");
compareString2()
compareString2("Anita", "Ben");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
compareString()
compareString2()
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/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
compareString()
30376498.0 Ops/sec
compareString2()
7690405.0 Ops/sec
Related benchmarks:
object compare
return the bigger value of two variables
lodash.isEqual vs fast-deep-equal
Test fast deep equal
Comparison Check (number VS string)
IIFE assignment vs mutable assignment
Nullish coalescing V.S. if/else (native)
Lodash isEqual vs shallowEqual test
lodash.isEqual vs dequal 2
Comments
Confirm delete:
Do you really want to delete benchmark?