Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare string refactor case sensitive
(version: 0)
Comparing performance of:
without lowercase function vs with lowercase function comp
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var wordList1 = ['apple', 'banana', 'orange', 'kiwi', 'grape', 'APPLE', 'BaNaNa', 'ORAnge', 'kiWI', 'GRapE']; var wordList2 = ['kiwi', 'orange', 'banana', 'grape', 'apple'];
Tests:
without lowercase function
function stringCompare(lowerA, lowerB) { if (lowerA < lowerB) { return -1; } if (lowerA > lowerB) { return 1; } return 0; } for (let i = 0; i < wordList1.length; i++) { for (let j = 0; j < wordList2.length; j++) { stringCompare(wordList1[i], wordList2[j]); } }
with lowercase function comp
function stringCompare(a, b) { const lowerA = a.toLowerCase(); const lowerB = b.toLowerCase(); if (lowerA < lowerB) { return -1; } if (lowerA > lowerB) { return 1; } return 0; } for (let i = 0; i < wordList1.length; i++) { for (let j = 0; j < wordList2.length; j++) { stringCompare(wordList1[i], wordList2[j]); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
without lowercase function
with lowercase function comp
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/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
without lowercase function
160847.6 Ops/sec
with lowercase function comp
145892.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark is designed to compare the performance of two approaches for comparing strings in JavaScript: one approach uses a case-sensitive comparison, while the other approach converts both strings to lowercase using the `toLowerCase()` method. **Script Preparation Code** The script preparation code defines two arrays, `wordList1` and `wordList2`, which contain lists of words with varying cases (e.g., "apple" vs. "Apple"). The script also defines a comparison function, `stringCompare`, which takes two strings as input and returns an integer indicating their relative order. **Options Compared** The benchmark compares the performance of two options: 1. **Case-sensitive comparison**: This approach uses the original string values without any modifications. 2. **Case-insensitive comparison with lowercase conversion**: This approach converts both input strings to lowercase using the `toLowerCase()` method before comparing them. **Pros and Cons** * **Case-sensitive comparison**: + Pros: Faster performance, as no additional string conversions are required. + Cons: May produce incorrect results due to case differences between strings. * **Case-insensitive comparison with lowercase conversion**: + Pros: Produces consistent results regardless of case differences between strings. + Cons: Requires an additional step (string conversion) which may introduce performance overhead. **Library and Purpose** There is no library mentioned in the benchmark, as it only uses built-in JavaScript features. **Special JS Feature or Syntax** The `toLowerCase()` method is used to convert string values to lowercase. This is a standard JavaScript feature that converts a string to its lowest-case equivalent. **Other Considerations** * **Performance Impact**: The case-insensitive comparison approach may introduce some performance overhead due to the additional string conversion step. * **Code Readability and Maintainability**: The use of `toLowerCase()` method may make the code slightly more complex, as it adds an extra layer of abstraction. However, this is a trade-off for the benefits of consistent results. **Alternatives** If you need to compare strings in JavaScript, other approaches include: * Using the `localeCompare()` method (available in modern browsers), which compares strings based on the user's locale settings. * Implementing custom string comparison logic using regular expressions or other techniques. * Using a third-party library that provides efficient string comparison capabilities.
Related benchmarks:
Exclude keywords
Spread Operator VS Array.slice()
Searching in an array of objects
Javascript: Case insensitive string comparison indexOf vs includes 2
Comments
Confirm delete:
Do you really want to delete benchmark?