Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Multiple RegExp Replace
(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); return order===-1 ? word : replaceArr[order]; }; 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):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark defines two functions: `filter1` and `filter2`. Both functions are designed to replace specific substrings in a given text using regular expressions (regex). **filter1** `filter1` uses a regex pattern with a function as the replacement. The pattern is created by joining an array of words (`findArr`) into a single string, separated by pipes (`|`). This allows for more complex patterns and multiple conditions to be applied. ```javascript const findArr = [ 'рев', 'revнивый студент', 'ман', 'маньяк', 'студ', 'студент', 'гр', 'гражданин', 'лун', 'лунатик', 'док', 'доктор', // ... ]; const replaceArr = [ 'jeal', 'jeal', 'jeal', 'jeal', 'stud', 'stud', 'stud', 'stud', 'sleep', 'sleep', 'sleep', 'sleep', // ... ]; function replaceFunction(word) { const order = findArr.indexOf(word); return order === -1 ? word : replaceArr[order]; } const text = 'Ты рев, лунатик или дежурный ? ПустьEven Vahchter! Главное, что не мафиози.'; text = text.replace(new RegExp('(^|\\s)('+findArr.join('|')+')(\\s|$)', 'gi', replaceFunction)); ``` **filter2** `filter2` uses a more traditional approach with separate regex patterns for each word. This approach is often simpler and faster but may not be as flexible as `filter1`. ```javascript const text = 'Ты рев, лунатик или дежурный ? ПустьEven Vahchter! Главное, что не мафиози.'; text = text.replace(/(^|\\s)(рев|ревнивый студент|ман|маньяк)(\\s|$)/gi, '<span class="jeal"></span> '); // ... ``` **Comparison** The benchmark compares the execution performance of `filter1` and `filter2` on two test cases: "Filter By Function" and "Filter By Strings". **Pros and Cons** * **filter1**: + Pros: - More flexible and powerful pattern matching - Can handle multiple conditions with a single regex pattern + Cons: - May be slower due to the overhead of creating and executing the function - Requires more memory allocation for the `replaceArr` array * **filter2**: + Pros: - Faster execution speed due to simpler and faster pattern matching - Less memory allocation required + Cons: - Less flexible and powerful pattern matching - Requires separate regex patterns for each word **Device Platform and Browser** The benchmark results are displayed for a specific device platform (Desktop) and browser (Chromium 69). The results show the number of executions per second for each test case. Overall, the benchmark aims to evaluate the performance of two different approaches to text processing using regular expressions in JavaScript.
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?