Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Multiple RegExp Replacer2
(version: 0)
Comparing performance of:
Filter By Function vs Filter By Strings
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function filter1(message){ const findArr = [ 'рев', 'ревнивый студент', 'ман', 'маньяк', 'студ', 'студент', 'гр', 'гражданин', 'лун', 'лунатик', 'док', 'доктор', 'деж', 'дежурный', 'коп', 'комиссар', 'пом', 'помощник дежурного', 'серж', 'сержант', 'пох', 'похититель', 'маф', 'мафиози', 'глава похитителей', 'босс мафии', 'босс', 'котик', 'кот', 'вахтер', 'адвокат' ], replaceArr = [ 'jeal', 'jeal', 'jeal', 'jeal', 'stud', 'stud', 'stud', 'stud', 'sleep', 'sleep', 'sleep', 'sleep', 'duty', 'duty', 'duty', 'duty', 'assis', 'assis дежурного', 'assis', 'assis', 'robb', 'robb', 'robb', 'robb', 'hrobb', 'hrobb', 'hrobb', 'cat catm', 'cat', 'law', 'law' ], replaceFunction = word => { const order = findArr.indexOf(word.trim().toLowerCase()); return order===-1 ? word : ' <span class="'+replaceArr[order]+'"></span> '; }; message = message.replace(new RegExp('(^|\\s)('+findArr.join('|')+')(\\s|$)', 'gi'), replaceFunction); } function filter2(message){ message = message.replace(/(^|\s)(рев|ревнивый студент|ман|маньяк)(\s|$)/gi,' <span class="jeal"></span> '); message = message.replace(/(^|\s)(студ|студент|гр|гражданин)(\s|$)/gi,' <span class="stud"></span> '); message = message.replace(/(^|\s)(лун|лунатик|док|доктор)(\s|$)/gi,' <span class="sleep"></span> '); message = message.replace(/(^|\s)(деж|дежурный|коп|комиссар)(\s|$)/gi,' <span class="duty"></span> '); message = message.replace(/(^|\s)(пом|помощник дежурного|серж|сержант)(\s|$)/gi,' <span class="assis"></span> '); message = message.replace(/(^|\s)(пох|похититель|маф|мафиози)(\s|$)/gi,' <span class="robb"></span> '); message = message.replace(/(^|\s)(глава похитителей|босс мафии|босс)(\s|$)/gi,' <span class="hrobb"></span> '); message = message.replace(/(^|\s)котик(\s|$)/gi,' <span class="cat catm"></span> '); message = message.replace(/(^|\s)кот(\s|$)/gi,' <span class="cat"></span> '); message = message.replace(/(^|\s)(вахтер|адвокат)(\s|$)/gi,' <span class="law"></span> '); }
Tests:
Filter By Function
const text = 'Ты рев, лунатик или дежурный ? Пусть даже Вахтер! Главное, что не мафиози.'; filter1(text);
Filter By Strings
const text = 'Ты рев, лунатик или дежурный ? Пусть даже Вахтер! Главное, что не мафиози.'; filter2(text);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Filter By Function
Filter By Strings
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 provided benchmark and explain what's being tested, compared options, pros/cons of those approaches, and other considerations. **Benchmark Definition:** The provided JSON represents a JavaScript microbenchmark that tests the performance of two different methods for replacing substrings in a string: 1. `filter1` function: * Uses a regular expression (RegExp) with multiple capturing groups to replace all occurrences of a substring. * The replacement is done using a custom function `replaceFunction`, which takes the matched substring and returns the modified version with HTML span elements added around it. 2. `filter2` function: * Uses multiple separate RegEx patterns to replace specific substrings in the string. **Options being compared:** The benchmark compares the performance of these two approaches: 1. **Filter By Function (filter1)**: This approach uses a single function to replace all occurrences of substrings, leveraging the power of JavaScript's `replace()` method with a custom replacement function. 2. **Filter By Strings (filter2)**: This approach uses multiple separate RegEx patterns to replace specific substrings in the string. **Pros/Cons of each approach:** 1. **Filter By Function (filter1)**: * Pros: + More concise and readable code. + Leverages JavaScript's `replace()` method, which is optimized for performance. * Cons: + May be slower due to the overhead of function calls and object creation. 2. **Filter By Strings (filter2)**: * Pros: + Can be faster due to the direct use of RegEx patterns, which can take advantage of compiler optimizations. * Cons: + Code is more verbose and harder to maintain, especially for larger datasets or complex patterns. **Other considerations:** * Both approaches rely on JavaScript's `replace()` method, which is optimized for performance. However, the specific implementation details (e.g., use of capturing groups, lazy evaluation) might impact performance. * The benchmark only tests these two specific methods and does not account for other possible optimization techniques or variations in implementation. **Library usage:** None of the provided code uses any external libraries beyond JavaScript's built-in functionality. **Special JS features or syntax:** The `filter1` function utilizes a few advanced JavaScript features, including: * Arrow functions (`replaceFunction`) * Template literals (for string interpolation) * The `trim()` and `toLowerCase()` methods for substring manipulation These features are not essential to the benchmark's operation but contribute to the code's conciseness and readability. **Alternatives:** For similar benchmarks, you might consider comparing other approaches, such as: 1. Using a specialized library like RegEx or a custom implementation with native JavaScript functions (e.g., `String.prototype.replace()`). 2. Employing a different string manipulation technique, like using a Trie data structure to store and look up substrings. 3. Comparing the performance of different programming languages or environments (e.g., Node.js, WebAssembly). Keep in mind that these alternatives would require significant changes to the benchmark's implementation and scope.
Related benchmarks:
Unique via Set vs Filter
Word width calculation speed
match vs include
match vs include vs indexOf
test vs include vs indexOf no match
Comments
Confirm delete:
Do you really want to delete benchmark?