Home > front end >  How efficient in JS string efficient insertion string
How efficient in JS string efficient insertion string

Time:11-21

Let splices: [6, 11, 24],
Let strings: [' Big ', '. ', '1 oclock'],
Let bigtext=' ';
Let the STR=' ';
For (let c=1000000; C -;) {
Bigtext="Hello World" The time is ";
STR=' ';
Let cur=0;
For (the let I=0; i STR +=bigtext. Slice (cur, splices [I]) + strings [I];
Cur=. Splices [I];
}
}
Do replace one million times, and finally see time comparing efficiency
Finally printed the result should be a Hello Big World! The time is 1 oclock
Results boss thinks that the efficiency is too low, the consulting a great god and there is no way to improve the efficiency of a bit, I feel enough simplified, and then go down may be abandoned JS native string replacement function, to optimize the again, this is workable

CodePudding user response:

The purpose of didn't see you do this... You this is specially the turn in the string concatenation, there is no "within the string is inserted into the
"
Js in the string, the joining together of each method on the basis of different browsers and different efficiency, with much of it should be: +=, in IE used to have a use Array. The push () + Array. The join ()
  • Related