var a = "John Doe"
var b = "john doe"
var c = "john smith"
var o = {
sensitivity: 'base'
}
a.toLowerCase() === b.toLowerCase()
a.localeCompare(b,undefined,o)
c.toLowerCase() === b.toLowerCase()
c.localeCompare(b,undefined,o)
new RegExp(a + "|" + b, "i");
new RegExp(c + "|" + b, "i");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
toLowerCase() | |
localeCompare() | |
toLowerCase() FAIL | |
localeCompare() FAIL | |
Regex | |
Regex FAIL |
Test name | Executions per second |
---|---|
toLowerCase() | 68655248.0 Ops/sec |
localeCompare() | 281324.2 Ops/sec |
toLowerCase() FAIL | 41516644.0 Ops/sec |
localeCompare() FAIL | 299076.0 Ops/sec |
Regex | 7187089.5 Ops/sec |
Regex FAIL | 7211958.0 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to test the performance of JavaScript string comparison methods: toLowerCase()
, localeCompare()
(with options), and regular expressions (new RegExp
). The tests cover various scenarios, including case-insensitive comparisons, failures, and edge cases.
Options Comparison
There are three main approaches being compared:
toLowerCase()
: This method converts both strings to lowercase before comparing them.localeCompare()
(with options): This method uses the browser's locale settings to perform a case-insensitive comparison.sensitivity
: Can be set to 'base'
, 'variant'
, or 'custom'
.toLowerCase()
, but slower and more complex.new RegExp
): This method uses a regular expression to perform a case-insensitive comparison.Library/Functionality
None of the test cases use any external libraries or functions other than built-in JavaScript methods (toLowerCase()
and localeCompare()
) and regular expressions (new RegExp
).
Special JS Features/Syntax
There are no special JavaScript features or syntax being tested in this benchmark. The focus is on comparing different string comparison methods.
Other Alternatives
If you're interested in testing other string comparison methods, here are a few alternatives:
String.prototype.normalize()
with the 'NFD'
form to normalize strings before comparison.DOM-based comparison libraries
, such as string-regex
or normalize.js
.Custom comparison functions
, which can be used to test specific use cases or edge scenarios.In summary, this benchmark provides a comprehensive evaluation of three popular JavaScript string comparison methods: toLowerCase()
, localeCompare()
with options, and regular expressions. The results can help developers understand the performance differences between these approaches and make informed decisions about which method to use in their applications.