Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs search 2
(version: 0)
String searching
Comparing performance of:
indexOf vs search vs search (u) vs exec vs exec (U)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var NEWLINE = /\x0d\x0a|[\x0a\x0d\u2028\u2029]/; var NEWLINE_U = /\x0d\x0a|[\x0a\x0d\u2028\u2029]/u; var str = 'foobarbaz!'.repeat(1000) + '\n'; window.result = 0;
Tests:
indexOf
result += str.indexOf('\n');
search
result += str.search(NEWLINE);
search (u)
result += str.search(NEWLINE_U);
exec
const match = NEWLINE.exec(str); if (match) result += match.index;
exec (U)
const match = NEWLINE_U.exec(str); if (match) result += match.index;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
indexOf
search
search (u)
exec
exec (U)
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):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, library usage, special JS features, and alternatives. **Benchmark Overview** The benchmark compares four different string searching methods: `indexOf`, `search`, `exec`, and their Unicode versions (`search` and `exec`) using a predefined string `str`. The goal is to determine which method is the fastest. **String Preparation Code** The script preparation code defines two regex patterns: 1. `NEWLINE`: matches newline characters (`\x0d\x0a`) and line breaks (`[\x0a\u2028\u2029]`). 2. `NEWLINE_U`: an Unicode version of `NEWLINE`, using the `u` flag to enable Unicode support. The script also defines a long string `str` that contains repeated newline characters, which will be used for searching. **Test Cases** Each test case is defined by a JSON object with two properties: 1. `Benchmark Definition`: specifies the JavaScript code that performs the search operation. 2. `Test Name`: labels the test case. The four test cases are: 1. `indexOf` 2. `search` (with the `NEWLINE` regex) 3. `exec` (with the `NEWLINE` regex) 4. `exec (U)` (with the `NEWLINE_U` regex) **Comparison** The benchmark compares the execution time of each test case across different browsers and devices. **Options Compared** The options being compared are: 1. `indexOf`: uses the `indexOf()` method to search for a substring. 2. `search`: uses the `search()` method to search for a regular expression pattern in a string. 3. `exec`: uses the `exec()` method to execute a regular expression pattern and returns the first match. **Pros and Cons** Here's a brief summary of each option: 1. `indexOf`: * Pros: simple, widely supported, fast. * Cons: only searches for an exact substring match, not a regex pattern. 2. `search`: * Pros: can search for a regex pattern, relatively fast. * Cons: may be slower than `indexOf` for simple substring searches. 3. `exec`: * Pros: can execute a full regex pattern, returns the first match. * Cons: generally slower than `indexOf` and `search`. **Library Usage** None of the test cases use any external libraries. **Special JS Features** The benchmark uses some special JavaScript features: 1. The `u` flag is used to enable Unicode support in the `NEWLINE_U` regex pattern. 2. The `repeat()` method is used to create a long string with repeated newline characters. These features are widely supported across modern browsers, but may not be present in older or less-capable browsers. **Alternatives** Other alternatives for string searching include: 1. `match()` method: can search for a regex pattern and return all matches. 2. `regex.test()` method: can test a regex pattern against a string and return a boolean result. 3. Third-party libraries like jQuery's `regex` or other regex engines. However, the `indexOf`, `search`, and `exec` methods are generally more efficient and widely supported than these alternatives.
Related benchmarks:
index vs lastindexof startsWith
index vs lastindexof (last index)
indexOf vs search
indexOf vs search 3
Comments
Confirm delete:
Do you really want to delete benchmark?