Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Remove vs String Replace
(version: 0)
Comparing performance of:
String Replace No Compare vs String Replace With Compare vs Convert to Array vs Remove From Array
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'fred red bed said', searchTerm = 'bed', arr = str.split(' '); function removeSubstr(str, substr){ tmp = " " + str + " "; substr = " " + substr + " "; if (tmp.indexOf(substr) !== -1 ) { tmp = tmp.replace(substr, "").trim(); return tmp } return str; } function removeSubstr2(str, substr){ var len = str.length, tmp = (" " + str + " ").replace(" " + substr + " ", "").trim(); if ( str.length < len ) return tmp; return str; } function removeSubstr3(str, substr){ var strSpl = str.split(' '), idx = strSpl.indexOf(substr); if ( idx > -1) { return strSpl.splice(idx, 1).join(' '); } return str; } function removeElem(arr, substr){ var idx = arr.indexOf(substr); if ( idx > -1) { return arr.splice(idx, 1); } return arr; }
Tests:
String Replace No Compare
removeSubstr(str, searchTerm)
String Replace With Compare
removeSubstr2(str, searchTerm)
Convert to Array
removeSubstr3(str, searchTerm)
Remove From Array
removeElem(arr, searchTerm)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
String Replace No Compare
String Replace With Compare
Convert to Array
Remove From Array
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'd be happy to help you understand what's being tested on the provided JSON benchmark. The benchmark is testing four different approaches for removing a specific substring from either a string or an array: 1. **String Replace**: This approach uses the `replace()` method to remove the substring from the original string. * Pros: Simple and efficient, works well for most cases. * Cons: Can be slow if the substring is not found, as it requires a full string scan. 2. **String Replace with Compare**: This approach first checks if the substring exists in the string using `indexOf()`, and only then performs the replacement. * Pros: Reduces unnecessary work by checking for the existence of the substring before replacing it. * Cons: Adds an extra step, which can lead to a slight performance overhead. 3. **Convert to Array**: This approach converts the input string into an array using `split()`, and then uses the `indexOf()` method to find and remove the substring from the array. * Pros: Can be faster than the previous two approaches if the input is already an array or can be easily converted to one. * Cons: Requires additional memory allocation for the temporary array, which can be a concern for large inputs. 4. **Remove From Array**: This approach uses the `indexOf()` method to find the index of the substring in the input array, and then uses `splice()` to remove it from the array. * Pros: Efficiently removes the substring from the array without requiring additional memory allocation. * Cons: Requires the substring to be found in the array, which can lead to slower performance if the array is very large. The benchmark is comparing the execution times of these four approaches for different test cases. The results show that: * "Remove From Array" performs best with an average execution time of 179,087.328125 executions per second. * "String Replace With Compare" comes in second, with an average execution time of 124,368.90625 executions per second. * "String Replace No Compare" is slower, with an average execution time of 238,998.078125 executions per second. * "Convert to Array" has the slowest average execution time, at 52,087.328125 executions per second. It's worth noting that these results are specific to Chrome 52 on Windows and may not reflect performance differences on other browsers or platforms.
Related benchmarks:
Character removal
Trimming leading/trailing characters Bounds Fix
Trimming leading/trailing characters Bounds Fix2
Trimming leading/trailing characterssss
Comments
Confirm delete:
Do you really want to delete benchmark?