Agreed..! JS strings are immutable, however your concerns about their concatenation, which are time consuming, are not well-founded. I can confirm that both in Chrome, Firefox, Opera, IE11, and Safari a Array.prototype.join()operation on an array of 1,000,000 random characters takes about 10 ~ 40 ms depending on the engine. Yes ... like that. (especially Spider Monkey is shining). Take a look
var arr = [],
match = [],
longStr = "",
count = 1000000,
start = 0,
end = 0;
for (var i=0; i<count; i++){
arr.push((+new Date()*Math.random()).toString(36)[0]);
}
start = performance.now();
longStr = arr.join("");
end = performance.now();
console.log("Concatenation took "+(end-start)+" msecs");
// Concatenation took 10.875 msecs.. Wow Firefox..!
start = performance.now();
match = longStr.match(/7{5,}/g);
!match && (match = [])
end = performance.now();
console.log("Regex match took "+(end-start)+" msecs and found " +match.length+" matches as: ", match);
//Regex match took 6.550000000046566 msecs and found 1 matches as: ["77777"]
, , . . . , Array.prototype. , primitiveValue, , length, this.primitiveValue = this.join();, Array.prototype, length, primitiveValue. , . , , primitiveValue. , length , . RingBuffer.prototype , primitiveValue . .
function RingBuffer(){
this.primitiveValue = "";
this.__len;
Object.defineProperty(this, "length", {
enumerable: true,
configurable: true,
get: this.getLength,
set: this.setLength
});
}
RingBuffer.prototype = Array.prototype;
RingBuffer.prototype.constructor = RingBuffer;
RingBuffer.prototype.getLength = function(){
return this.__len;
};
RingBuffer.prototype.setLength = function(val){
this.__len = val;
this.primitiveValue = this.join("");
};
var ringu = new RingBuffer();
, ringu 100 000 . Chrome 49 :
var longStr = "",
count = 100000,
start = performance.now(),
end = 0;
for (var i=1; i<=count; i++){
ringu.push((+new Date()*Math.random()).toString(36)[0]);
if (!(i % 10000)){
end = performance.now();
console.log(i/10000+". 10000 pushes done at :"+(end - start)+" msecs");
start = end;
}
}
console.log("ringu is filled with " + count + " random characters");
start = performance.now();
longStr = ringu.join("");
end = performance.now();
console.log("Last concatenation took "+(end-start)+" msecs");
1. 10000 pushes done at :1680.6399999996647 msecs
2. 10000 pushes done at :4873.2599999997765 msecs
3. 10000 pushes done at :8044.155000000261 msecs
4. 10000 pushes done at :11585.525000000373 msecs
5. 10000 pushes done at :14642.490000000224 msecs
6. 10000 pushes done at :17998.389999999665 msecs
7. 10000 pushes done at :20814.979999999516 msecs
8. 10000 pushes done at :24024.445000000298 msecs
9. 10000 pushes done at :27146.375 msecs
10. 10000 pushes done at :30347.794999999925 msecs
ringu is filled with 100000 random characters
Last concatenation took 3.510000000707805 msecs
, , primitiveValue , , this.join("");. 500K RingBuffer 30 .
... SpiderMonkey. , Node.Js, , JXCore, Spider Monkey ChakraCore Node V8.
1. 10000 pushes done at :710.310000000005 msecs
2. 10000 pushes done at :1831.4599999999991 msecs
3. 10000 pushes done at :3018.199999999997 msecs
4. 10000 pushes done at :4113.779999999999 msecs
5. 10000 pushes done at :5144.470000000008 msecs
6. 10000 pushes done at :6588.179999999993 msecs
7. 10000 pushes done at :7860.005000000005 msecs
8. 10000 pushes done at :8727.050000000003 msecs
9. 10000 pushes done at :9795.709999999992 msecs
10. 10000 pushes done at :10866.055000000008 msecs
ringu is filled with 100000 random characters
Last concatenation took 1.0999999999912689 msecs