Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Multiple RegExp Replacer
(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):
I'll break down the provided JSON and explain what's being tested, compared, and some pros and cons of each approach. **Benchmark Definition** The benchmark measures the performance difference between two approaches to replace words in a text: 1. `filter1` (JavaScript function): Replaces words using a regular expression with a custom replacement function. 2. `filter2` (JavaScript strings): Replaces words using multiple string replacements. **Options compared** * `filter1` uses a single regular expression with a custom replacement function to replace all occurrences of the specified words. * `filter2` uses multiple string replacements, each targeting a specific word or phrase. **Pros and Cons** * **filter1**: + Pros: More flexible and efficient, as it only compiles the regex once and can handle more complex patterns. + Cons: May be slower due to the overhead of creating the replacement function and compiling the regex. * **filter2**: + Pros: Can be faster, as each replacement is a simple string match without the need for regex compilation or creation of a custom function. + Cons: Less flexible and may require more code to manage multiple replacements. **Library and purpose** None mentioned in the provided JSON. However, based on the code, it appears that no external library is used for this benchmark. **Special JS features or syntax** The `filter1` function uses a custom replacement function (`replaceFunction`) which is not a standard JavaScript feature. The use of `const order = findArr.indexOf(word);` to get the index of the word in `findArr` is also not a standard feature, but rather an optimization technique. **Other alternatives** * Other approaches could be: + Using a more efficient string replacement library or module (e.g., `replace-regex`, `string-replace-all`). + Using a different regex pattern or syntax to improve performance. + Optimizing the code using techniques like memoization, caching, or parallel processing. Please note that this analysis is based on the provided JSON and may not be exhaustive or definitive. Additional context or information might be required to fully understand the benchmark's requirements and optimization strategies.
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?