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 === b
a.localeCompare(b, 'en', config);
a === c
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 | 18784930.0 Ops/sec |
locale compare true | 316374.9 Ops/sec |
=== false | 18740000.0 Ops/sec |
locale compare false | 262599.6 Ops/sec |
I'll provide an explanation of the benchmark, options compared, pros and cons, and other considerations.
Benchmark Definition:
The benchmark compares three different approaches to checking if two strings are equal:
a === b
): This method checks for exact equality between two strings using the ===
operator.a.localeCompare(b, 'en', config);
): This method uses the localeCompare()
function to compare two strings in a case-insensitive manner, with a specific locale ('en') and sensitivity setting.Individual Test Cases:
The benchmark consists of four test cases:
a === b
): The test checks if two identical strings are equal using strict equality.a.localeCompare(b, 'en', config);
): The test checks if two identical strings are equal using locale-comparative with English locale and base sensitivity.a === c
): The test checks if two different strings are equal using strict equality (i.e., they should not be considered equal).a.localeCompare(c, 'en', config);
): The test checks if two different strings are equal using locale-comparative with English locale and base sensitivity.Options Compared:
The benchmark compares the performance of:
a === b
)a.localeCompare(b, 'en', config)
)Pros and Cons:
a === b
):a.localeCompare(b, 'en', config);
):Other Considerations:
config
object in the benchmark definition determines the sensitivity setting for locale-comparative comparisons.By comparing these two approaches, the benchmark provides a comprehensive view of the trade-offs between performance and accuracy in string comparison scenarios.