function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
var data = Array(10000).fill(Array(40).fill(makeid(1000)))
var rows = []
var row = {}
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item}); rows.push(row)})
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item.normalize()}); rows.push(row)})
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item.trim()}); rows.push(row)})
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item.normalize().trim()}); rows.push(row)})
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item.trim().normalize()}); rows.push(row)})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
no | |
normalize | |
trim | |
both 1 | |
both 2 |
Test name | Executions per second |
---|---|
no | 50.4 Ops/sec |
normalize | 3.0 Ops/sec |
trim | 37.0 Ops/sec |
both 1 | 3.0 Ops/sec |
both 2 | 3.0 Ops/sec |
The benchmark defined in the provided JSON tests the performance of different approaches to normalize and trim text in JavaScript. It evaluates several methods of processing an array of text data containing long strings. Here’s a breakdown of the tests conducted, what they compare, and some implications regarding their performance and usability.
No Processing:
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item}); rows.push(row)})
Trimming:
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item.trim()}); rows.push(row)})
String.prototype.trim()
method, which removes whitespace from both ends of a string.Normalizing:
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item.normalize()}); rows.push(row)})
String.prototype.normalize()
method, which can convert strings to a canonical form, dealing with combined characters or different representations of the same character.Both Normalize and Trim (Order 1):
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item.normalize().trim()}); rows.push(row)})
Both Normalize and Trim (Order 2):
data.forEach((items) => {items.forEach((item,i)=>{row[i] = item.trim().normalize()}); rows.push(row)})
normalize()
method.String.prototype.trim()
:
String.prototype.normalize()
:
Other alternatives for similar string processing might include:
item.replace(/^\s+|\s+$/g, '')
, although this may be less readable and slower in some cases.In summary, this benchmark effectively illustrates the trade-offs involved in using different string processing methods to normalize and trim text. Understanding these trade-offs is crucial for software engineers when optimizing applications for performance while ensuring correct data handling.