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'
};
a.toLowerCase() === b.toLowerCase()
a.localeCompare(b, 'en', config);
a.toLowerCase() === c.toLowerCase()
a.localeCompare(c, 'en', config);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
=== true | |
locale compare true | |
=== false | |
locale compare false |
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 |
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:
a.toLowerCase() === b.toLowerCase()
: Compares the lowercase versions of a
and b
.a.localeCompare(b, 'en', config);
: Uses the locale-aware comparison function with English language settings to compare a
and b
.a.toLowerCase() === c.toLowerCase()
: Compares the lowercase versions of a
and c
, expecting a false result due to the typo in c
.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:
toLowerCase()
localeCompare()
toLowerCase()
.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.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.