Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endOf vs cascading equality
(version: 0)
Test whether it is prohibitive to use endsWith to match a class of strings vs listing all of those items in an if
Comparing performance of:
chained equality check vs endsWith class matching
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const prefixes = ["apple", "orange", "mango", "banana", "strawberry", "tomato", "cherry", "grape", "onion", "watermellon"]; const suffixes = ["bad", "good"]; const rand = (n) => Math.floor(Math.random() * n); const randomStringCount = 1000000; const randomBufferOfStrings = new Array(randomStringCount); for (let i = 0; i < randomStringCount; i++) { randomBufferOfStrings[i] = prefixes[rand(prefixes.length)] + suffixes[rand(suffixes.length)]; } window.randomBufferOfStrings = randomBufferOfStrings;
Tests:
chained equality check
let badCount = 0; for (let str of randomBufferOfStrings) { if (str === "applebad" || str === "orangebad" || str === "mangobad" || str === "bananabad" || str === "strawberrybad" || str === "tomatobad" || str === "cherrybad" || str === "grapebad" || str === "onionbad" || str === "watermellonbad") { badCount++; } } console.log(badCount);
endsWith class matching
let badCount = 0; for (let str of randomBufferOfStrings) { if (str.endsWith("bad")) { badCount++; } } console.log(badCount);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
chained equality check
endsWith class matching
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark compares two approaches to match strings: using `endsWith` (class matching) versus listing all possible suffixes in an if statement. **Approaches Compared** 1. **Chained Equality Check**: This approach uses a long series of if-conditional statements to check if each string ends with a specific suffix. 2. **endsWith Class Matching**: This approach uses the built-in `endsWith` method, which is optimized by the JavaScript engine for performance. **Pros and Cons** * **Chained Equality Check**: + Pros: Easy to implement and understand. + Cons: Performance-intensive due to the number of conditional checks required. * **endsWith Class Matching**: + Pros: Optimized by the JavaScript engine, resulting in better performance. + Cons: May be less intuitive for developers unfamiliar with this method. **Library/Functionality Used** None. This benchmark uses built-in JavaScript functionality and does not require any external libraries. **Special JS Feature/Syntax** The `endsWith` method is a special feature introduced in ECMAScript 2015 (ES6) as part of the standardization effort to improve performance and readability. **Other Considerations** * The benchmark creates a large array of random strings, which helps to ensure that the results are representative of real-world scenarios. * The use of `Math.floor(Math.random() * n)` generates pseudo-random numbers for simulation purposes. * The benchmark's focus on comparing two specific approaches allows users to evaluate their performance and make informed decisions about which method to use in their own code. **Alternatives** Other alternatives to compare string matching methods might include: 1. Using regular expressions (regex) instead of `endsWith`. 2. Implementing a custom string matching function using bitwise operations. 3. Comparing the performance of different suffix arrays or trie data structures for efficient string matching. These alternatives may be more suitable depending on specific requirements and use cases, but they are not directly relevant to this particular benchmark.
Related benchmarks:
Compare TextEncoder, Blob, new TextEncoder
Encode vs Blob vs length
Encode vs Blob on large string
Blob vs TextEncoder
Comments
Confirm delete:
Do you really want to delete benchmark?