Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Character removal
(version: 0)
Two approaches to remove characters from a string
Comparing performance of:
Using split + filter + join vs Using RegExp global replace
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = "this is a string with speciål chäräcters to remöve"; var badchars = ['å', 'ä', 'ö']; function removeChars1 (str, badchars) { return str.split("").filter((chr) => badchars.indexOf(chr) === -1).join(""); } function removeChars2 (str, badchars) { return str.replace( new RegExp("(" + badchars.join('|') + ")", "g"), "" ); }
Tests:
Using split + filter + join
var final = removeChars1(str, badchars);
Using RegExp global replace
var final = removeChars2(str, badchars);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using split + filter + join
Using RegExp global replace
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 benchmark and its options. **Benchmark Description** The benchmark compares two approaches to remove characters from a string: 1. **Using `split()`, `filter()`, and `join()`**: This approach splits the input string into an array of substrings using spaces as separators, filters out the elements that contain characters in the `badchars` array, and then joins them back together without any separator. 2. **Using `RegExp` global replace**: This approach uses a regular expression to match all occurrences of characters in the `badchars` array and replaces them with an empty string. **Options Compared** The two options are compared in terms of performance, specifically: * How many executions per second can each option handle on a given machine. * Which option is faster for removing a set of characters from a string. **Pros and Cons** 1. **Using `split()`, `filter()`, and `join()`**: * Pros: This approach is more explicit, readable, and easy to understand, especially for developers familiar with array operations in JavaScript. * Cons: It can be slower due to the overhead of creating an array, filtering it, and joining it back together. Additionally, it requires more memory allocation and garbage collection. 2. **Using `RegExp` global replace**: * Pros: This approach is often faster because regular expressions are optimized for performance in JavaScript engines. It also uses less memory and avoids the overhead of creating an array. * Cons: The code can be harder to read and understand, especially for developers unfamiliar with regular expressions. **Library Used** In this benchmark, `RegExp` is used as a built-in library in JavaScript. Regular expressions are a powerful pattern-matching system that allows you to search for patterns in strings and extract or modify data based on those patterns. **Special JS Feature/Syntax** None of the options use any special JavaScript features or syntax beyond what's standard in modern JavaScript implementations.
Related benchmarks:
regex vs chars
Regex remove whitespace vs trim
regex vs filter
regex vs filter 2
Comments
Confirm delete:
Do you really want to delete benchmark?