The provided content discusses the introduction and advantages of the padStart() and padEnd() string methods in JavaScript, which were added in ECMAScript2017, offering a performance boost compared to traditional padding techniques.
Abstract
The web content introduces JavaScript developers to the ES2017 padStart() and padEnd() methods, which simplify the process of padding strings to a specified length with whitespace or custom characters. It explains the concept of string padding and its practical applications, such as aligning text in monospace font or standardizing data input. The article emphasizes the performance benefits of these methods, citing tests that show they are significantly faster than previous padding techniques and even faster than the MDN-recommended polyfill. The author concludes by encouraging the adoption of these modern JavaScript features for efficient string manipulation.
Opinions
The author believes that the new padding methods padStart() and padEnd() make string manipulation in JavaScript easier and more efficient.
The author suggests that using these modern features can lead to better performance, citing that padStart() is approximately ten times faster than traditional dynamic string concatenation.
There is an opinion that the performance gain from using padStart() and padEnd() is substantial enough to outweigh the convenience of older methods.
The author acknowledges the existence of a polyfill for older JavaScript versions but points out that even the polyfill is slower than the native implementation of padStart().
The article implies that staying current with JavaScript features, such as padStart() and padEnd(), can improve both the developer experience and code performance.
The author hints at a preference for modern JavaScript features by highlighting the performance advantages and ease of use of padStart() and padEnd().
How to pad a string in JavaScript
ES2017 added the padStart() and padEnd() methods, which add whitespace or other characters to a string until it reaches a specific length.
The question with any new feature added to JavaScript is whether it runs slower. Losing performance may not be worth the convenience.
I wondered if the .padStart() method would be slower than the old way.
To find out, I ran some test cases using jsPerf, a free tool to test JavaScript performance, in order to compare string padding to string concatenation.
I compared .padStart() to dynamically padding by creating an array and then joining it into a string using a whitespace character.
Here are the results, showing a clear 1st, 2nd, and 3rd place:
Not only was .padStart() not slower than padding with dynamic concatenation of an array, .padStart() was actually 10x faster.
Of course, if there is a case where one can use static string concatenation, without needing to dynamically create a string, that would result in another 100x speed boost over padStart().
I did not test padEnd(), though it should work the same as padStart().
I admit it, the Array method seemed like a bit of a hack. Surely there would be a better way to pad strings in older version of JavaScript, and that way would surely be much faster. So I set out to find a faster algorithm.