const diacritics = '€âàáãåçñéêëèíîïìÀÁÃÅÑØøÉÊÈÍÎÏÌðýµÐÝ×ô¦òóõûùúÿÔÒÓÕÛÙÚÄÖÜÏßäöüïąĄęĘÓóćĆłŁńŃŚśŻżźŹ'.split('');
let value = 'Long Ócompany nameÓ before can replaceÓ';
for(let i = 0, l = diacritics.length; i < l; i++)
value = value.replace(diacritics[i], `${diacritics[i]}•`);
const diacritics2 = /([€âàáãåçñéêëèíîïìÀÁÃÅÑØøÉÊÈÍÎÏÌðýµÐÝ×ô¦òóõûùúÿÔÒÓÕÛÙÚÄÖÜÏßäöüïąĄęĘÓóćĆłŁńŃŚśŻżźŹ])/g;
let value2 = 'Long Ócompany nameÓ before can replaceÓ';
value2 = value2.replace(diacritics2, `$1${'•'}`);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
no regex | |
regex |
Test name | Executions per second |
---|---|
no regex | 211862.9 Ops/sec |
regex | 2030122.4 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to measure the performance of replacing strings in an array using two different approaches:
diacritics
string and replace it with the same character followed by a Unicode escape sequence (''
).diacritics
string.Options Compared
The two approaches have different pros and cons:
Library/Functionality Used
In this benchmark, we're using:
split()
method to split the diacritics
string into an array of individual characters.replace()
method to replace each character with the same character followed by a Unicode escape sequence (''
)./g
flag at the end of the regex pattern to enable global matching.Special JavaScript Features/Syntax
There are two special features used in this benchmark:
notation is used to insert a Unicode character (U+200C, a non-breaking space) into the replacement string./g
flag at the end of the regex pattern enables global matching, which allows the regex engine to replace all occurrences of characters in the diacritics
string.Other Alternatives
If you were to rewrite this benchmark using other approaches, here are some alternatives:
regex- escape
or unicode-escape
to handle Unicode escape sequences.Set
or Map
, to store the characters in the diacritics
string.Overall, this benchmark provides a good comparison of two approaches to replacing strings in an array, and can be used to help developers understand the trade-offs between simplicity and performance.