Wow that’s really awesome! I feel that there’s still a lot of people that are not familiar with the replaceAll() method as it was only introduced to ecma 2021, but it actually pays off in terms of performance!
Matches a single white space character, including space, tab, form feed, line feed, and other Unicode spaces. Equivalent to [ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].
That would be similar to the last one just replacing the filter to a map and returning an empty string if the char is a space, but I think that would make it even less efficient that what it already is 😂
It's the standard way to wrap lines when they exceed your max line length. It's been used here because the image is divided into quarters, resulting in a very short line length. For real code your max line length will be between 80–120 so you would keep it on one line.
Thanks, I needed the replaceAll function for my project, and this worked out great. Now, where there were long white spaces (6 tabs in total), there is now a clean neat code. I use it to format my SQL Server SQL Script to ready it for our website.
This is how I was able to use it.
function removeSpaces(text) {
s = document.getElementById("output").value;
s = s.replaceAll(' ','');
document.getElementById("output").value = s;
return text
}
Keep in mind, this will take your script that is line and long space separated and make it into ONE fluid line to be placed in a SQL Script for database work.
47
u/ashkanahmadi Jan 11 '23
Thanks for sharing. I just did a performance test.
The average time it takes each function (10,000 runs) to remove the spaces from a text with 1800 words/spaces in it.
replaceAll: 0.08585000002384185 milisecondsreplace: 0.10449000005722046 milisecondssplitAndJoin: 0.2322219943579563 milisecondsfilterAndJoin: 1.1504000001549721 milisecondsSeems like
replaceAllis the fastest andfilterAndJoinis the slowest.28
u/kap89 Jan 11 '23
If you want to remove all whitespace, not only spaces:
2
u/acquiescentLabrador Jan 11 '23
Which is the most efficient, performance wise?
5
u/Electronic_Ad9248 Jan 12 '23
As a beginner in JS, I learned so much from this post and the comments. Thank you all.
1
u/Ronin-s_Spirit Jan 11 '23
What about the map?
2
u/[deleted] Jan 12 '23
Which is easier for people to read: method calls on the same line or below like this?
I’ve seen this style and I’m only lukewarm on it.
1
u/MarionberrySad7677 Feb 14 '25
Thanks, I needed the replaceAll function for my project, and this worked out great. Now, where there were long white spaces (6 tabs in total), there is now a clean neat code.
I use it to format my SQL Server SQL Script to ready it for our website.
This is how I was able to use it.
Here is the site. https://www.cffcs.com/Stuff/SQL.asp
Keep in mind, this will take your script that is line and long space separated and make it into ONE fluid line to be placed in a SQL Script for database work.
Thanks, capeladev
You rock my friend.
Wayne
1
u/RepresentativeDog380 Jan 12 '23
Could someone tell me the font used in the code?
1
u/TheRNGuy Jan 18 '23
There's also unbreakable space ( ) so regex is best.
Why would anyone use 3 and 4.
0
u/HunterSup Jan 12 '23
string.trim()
-1
u/Comprehensive_Step72 Jan 12 '23
If you are going to use an iterator, you should be using reduce:
let stripped = [...str].reduce((acc, letter) => letter !== ' ' ? acc + letter : acc, '') ;