Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
blackList vs regexp2 - expanded
(version: 0)
Comparing performance of:
array search vs regexp
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var blackList = ["cazzo","madonna","stronzo","puttana","troia","porca","dio","porco","sticazzi"] var regexp = /^(cazzo|madonna|stronzo|puttana|troia|porca|dio|porco|sticazzi)$/ var array = ["hi", "hello"] for (var i = 0; i < 1000; i++) { array[i] = makeid(8) } function makeid(length) { var result = ''; var characters = 'abcdefghijklmnopqrstuvwxyz'; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; }
Tests:
array search
for (let i = 0; i < array.length; i++) { let result = blackList.indexOf(array[i]) >= -1; }
regexp
for (let i = 0; i < array.length; i++) { let result = array[i].match(regexp); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array search
regexp
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 benchmark test on MeasureThat.net, which compares the performance of two approaches: using an array's `indexOf` method and using a regular expression (`regexp`) to search for substrings in an array. **Test Case 1: Array Search** * **Benchmark Definition**: The first test case uses the following JavaScript code as its benchmark definition: ```javascript for (let i = 0; i < array.length; i++) { let result = blackList.indexOf(array[i]) >= -1; } ``` This code iterates over an array (`array`) and checks if each element exists in a predefined blacklist (`blackList`) using the `indexOf` method. The `result` variable is set to `true` if the element exists, and `false` otherwise. **Test Case 2: Regexp** * **Benchmark Definition**: The second test case uses the following JavaScript code as its benchmark definition: ```javascript for (let i = 0; i < array.length; i++) { let result = array[i].match(regexp); } ``` This code iterates over the same `array` and attempts to match each element against a regular expression (`regexp`). The `result` variable is an array containing matches or null if no match was found. **Pros and Cons** * **Array Search (indexOf)**: + Pros: - Fast lookup times, especially for large arrays. - Simple and efficient implementation. + Cons: - Requires the element to be in the `blackList` array; otherwise, returns `-1`. - May not handle edge cases or null values well. * **Regexp**: + Pros: - Flexible pattern matching with support for character classes, anchors, and more. - Can handle complex regular expressions. + Cons: - Generally slower than `indexOf` due to the overhead of compiling and executing regular expressions. - More complex implementation. **Library: RegExp** The regular expression (`regexp`) is a built-in JavaScript library that provides support for pattern matching using regular expressions. It's used to define the pattern to match against in the second test case. **Special JS Feature: Template Literals (String Interpolation)** Both benchmark definitions use template literals (strings with `$`-expressions) to create string variables, such as `blackList`. This feature allows you to embed expressions inside strings using backticks (`). It's a modern JavaScript feature introduced in ECMAScript 2015. **Other Alternatives** If the developers wanted to compare other approaches, they could have added additional test cases with different techniques, such as: * Using a `Set` data structure for fast lookup. * Implementing a custom search algorithm or binary search. * Utilizing Web APIs like Web Storage or IndexedDB for storing and searching data. These alternatives would require significant changes to the benchmark definitions and might not provide meaningful comparisons without proper testing.
Related benchmarks:
blackList vs regexp
blackList vs regexp2
Alphanumeric String test 2
regex vs js - not Alphanumeric String
Comments
Confirm delete:
Do you really want to delete benchmark?