Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs endWith
(version: 0)
Comparing performance of:
regex end vs endsWith
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexEnd = /https?:\/\/$/; window.match1 = 'https://'; window.match2 = 'http://'; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
regex end
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var regex = window.regexEnd; while (x < TOTAL_STRINGS) { const str = data[x]; regex.test(str); x += 1; }
endsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match1 = window.match1; var match2 = window.match2; while (x < TOTAL_STRINGS) { const str = data[x]; str.endsWith(match1) === 0 || str.endsWith(match2) === 0; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
regex end
endsWith
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):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Description** The provided JSON defines two benchmark tests: 1. **regex vs endWith**: This test compares the performance of two string matching methods: * `regex`: Using a regular expression (`window.regexEnd`) to match strings. * `endsWith`: Using the `String.prototype.endsWith()` method (not explicitly defined, but implied by the code) to check if a string ends with a certain substring. **Comparison Options** The benchmark tests compare the performance of these two methods: * **Regex**: The regular expression is defined as `window.regexEnd = /https?:\\/\\/$/;`, which matches URLs that end with `http://` or `https://`. * **endsWith**: This method checks if a string ends with a certain substring, in this case, either `match1` (`'https://'`) or `match2` (`'http://'`). **Pros and Cons of Each Approach** **Regex:** Pros: * Can match more complex patterns (e.g., phone numbers, email addresses) * Can be faster for larger strings Cons: * Can be slower for simple string matching due to the overhead of compiling and executing the regular expression * May have performance issues with very large or complex regex patterns **endsWith:** Pros: * Generally faster than regex for simple string matching * More straightforward and easier to implement Cons: * Only matches strings that end with a specific substring (in this case, either `match1` or `match2`) * May not be suitable for more complex pattern matching **Library Usage** There is no explicit library usage in the benchmark code. However, it's worth noting that the `String.prototype.endsWith()` method is a built-in JavaScript method, and its performance can vary depending on the browser and implementation. **Special JS Features or Syntax** This benchmark doesn't use any special JavaScript features or syntax beyond what's standard in modern JavaScript. If you're interested in exploring more advanced topics, MeasureThat.net has many other benchmarks that cover various aspects of JavaScript performance. **Other Alternatives** For similar string matching benchmarks, you might want to explore other microbenchmarking frameworks, such as: * `Benchmark.js` * `js-benchmark` * `microbenchmark` These frameworks offer a wide range of features and options for comparing the performance of different algorithms and implementations.
Related benchmarks:
Regex vs .startsWith
Normalize path: JS Regex vs .endsWith vs .indexOf vs .slice
JS Regex vs .startsWith vs .indexOf Jan
JavaScript: Regex vs .startsWith vs .indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?