Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
String comparison functions, if vs array reduce & Math.sign
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser:
Chrome 133
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
compareString()
173887632.0 Ops/sec
compareString2()
17714498.0 Ops/sec
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");