Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare string equals performance with case long strings
(version: 0)
Comparing performance of:
=== true vs locale compare true vs === false vs locale compare false
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.'; var b = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.'; var c = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H.'; var config = { sensitivity: 'base' };
Tests:
=== true
a.toLowerCase() === b.toLowerCase()
locale compare true
a.localeCompare(b, 'en', config);
=== false
a.toLowerCase() === c.toLowerCase()
locale compare false
a.localeCompare(c, 'en', config);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
=== true
locale compare true
=== false
locale compare false
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
=== true
4133101.2 Ops/sec
locale compare true
332901.8 Ops/sec
=== false
3912287.8 Ops/sec
locale compare false
274377.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the benchmark and its various components. **Benchmark Definition JSON** The provided JSON represents a JavaScript microbenchmark called "Compare string equals performance with case long strings". This benchmark measures the performance of three different approaches for comparing two strings, `a` and `b`, and their longer version, `c`. **Script Preparation Code** The script preparation code generates two very long, identical strings, `a` and `b`, containing a piece of classical Latin literature. The same string is generated with a typo in the last line (`'H.'` instead of `'H Rackham'`). Another identical string, `c`, is created with another minor typo. **Html Preparation Code** There is no HTML preparation code provided for this benchmark, so we won't be exploring that aspect further. **Individual Test Cases** The individual test cases are defined as an array of objects, each representing a separate test. There are four test cases: 1. `a.toLowerCase() === b.toLowerCase()`: Compares the lowercase versions of `a` and `b`. 2. `a.localeCompare(b, 'en', config);`: Uses the locale-aware comparison function with English language settings to compare `a` and `b`. 3. `a.toLowerCase() === c.toLowerCase()`: Compares the lowercase versions of `a` and `c`, expecting a false result due to the typo in `c`. 4. `a.localeCompare(c, 'en', config);`: Uses locale-aware comparison with English settings to compare `a` and `c`. **Options Compared** The two main options being compared are: * `toLowerCase()` vs `localeCompare()` * `a.toLowerCase() === b.toLowerCase()` vs `a.localeCompare(b, 'en', config)` **Pros and Cons of Each Approach:** 1. **`toLowerCase()`** * Pros: + Simple and efficient. + Works well for case-insensitive comparisons. * Cons: + Can be slow for large strings due to the overhead of converting to lowercase. 2. **`localeCompare()`** * Pros: + Provides locale-aware, case-sensitive comparison, which can be more accurate than `toLowerCase()`. + Can handle non-ASCII characters and cultural variations in string sorting. * Cons: + May be slower due to the overhead of language settings and character normalization. **Latest Benchmark Result** The latest benchmark results show varying execution times for each test case, depending on the browser and device platform. The results indicate that: * `toLowerCase()` is generally faster than `localeCompare()`, but with increasing variance in performance. * The comparison between `a` and `c` (with typos) is consistently slower than both `a` and `b`. Overall, this benchmark highlights the importance of considering locale-aware comparison when working with strings that may contain non-ASCII characters or cultural variations. While simple approaches like `toLowerCase()` can be efficient, they may not always provide accurate results in certain contexts.
Related benchmarks:
Reduce w/ Lowercase vs. Magic Regex
lodash startsWith vs native startsWith
Compare string equals performance without case diffs long strings
Compare string equals performance with case long strings: with IntlCollator
Comments
Confirm delete:
Do you really want to delete benchmark?