Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array vs regex 3
(version: 0)
Comparing performance of:
Array vs Regex vs Array2 vs Regex2
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = ['devlocal', 'dev', 'sim', 'beta', 'proddr', 'prod']; var regex = /^(devlocal|dev|sim|beta|proddr|prod)$/;
Tests:
Array
array.includes('prod')
Regex
regex.test('prod')
Array2
['devlocal', 'dev', 'sim', 'beta', 'proddr', 'prod'].includes('prod')
Regex2
/^(devlocal|dev|sim|beta|proddr|prod)$/.test('prod')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array
Regex
Array2
Regex2
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 and explain what's being tested on MeasureThat.net. **Benchmark Definition JSON** The provided benchmark definition represents two approaches: using an array and using a regular expression (regex) to test if a string contains the substring "prod". **Options Compared** In this benchmark, we have four test cases: 1. `Array.includes('prod')` 2. `regex.test('prod')` 3. `['devlocal', 'dev', 'sim', 'beta', 'proddr', 'prod'].includes('prod')` 4. `/^(devlocal|dev|sim|beta|proddr|prod)$/.test('prod')` The options being compared are: * Using the built-in `includes()` method on an array * Using the `test()` method on a regex object * Using the `includes()` method on an array (the same as option 1) * Using a regular expression with the `/^` and `$/` anchors to match the entire string **Pros and Cons of Each Approach** Here's a brief overview of each approach: 1. **Array.includes('prod')**: This is the most straightforward way to test if an array contains a specific element. However, it may be slower than other approaches due to the overhead of searching through the array. 2. **regex.test('prod')**: This method is generally faster than using `includes()` on an array because regex engines are optimized for pattern matching. However, it requires creating a new regex object, which can incur additional overhead. 3. **['devlocal', 'dev', 'sim', 'beta', 'proddr', 'prod'].includes('prod')**: This approach is similar to option 1 but uses an array instead of creating a new regex object. It's likely to be slower than using `test()` on a regex object due to the overhead of searching through the array. 4. **/(devlocal|dev|sim|beta|proddr|prod)$/.test('prod')**: This approach is likely to be the slowest because it requires creating a complex regular expression with multiple alternatives and anchors. **Library Used** None of the provided test cases use any external libraries. **Special JS Features or Syntax** The `includes()` method was introduced in ECMAScript 2019 (ES2020), so this benchmark is testing a relatively recent JavaScript feature. The `/^` and `$/` anchors are also part of the regex syntax, but they're not specific to modern browsers. **Other Alternatives** If you want to test these benchmarks on other environments or platforms, you can use online tools like: * jsPerf: A web-based benchmarking tool that allows you to compare performance between different JavaScript implementations. * BenchmarkJS: A library for creating and running microbenchmarks in Node.js and the browser. * Chrome DevTools' Performance tab: Allows you to run your own benchmarks and measure their performance. Keep in mind that these tools may have different testing frameworks or syntax than MeasureThat.net, so the results might not be directly comparable.
Related benchmarks:
array vs regex 2
RegEx vs Array.includes
RegEx vs Array.includes v2
RegEx.test vs Array.includes — fork 1
Comments
Confirm delete:
Do you really want to delete benchmark?