Arhip ([info]arhip) wrote,
@ 2006-08-21 14:58:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Entry tags:programming

Efficient string concatenation in JS
/*************************************************************
* Function : StringBuffer object
* Methods :
* append(string)
* toString() - returns string
* length() - returns count of array elements
* clear() - removes all from array
* Constructor : var buf = new StringBuffer();
* buf.append("a");
* buf.append("b");
* buf.append("c");
* buf.toString(); // returns "abc"
*************************************************************/


function StringBuffer() {
this.buffer = [];
}
StringBuffer.prototype.append = function append(string) {
this.buffer.push(string);
return this;
};
StringBuffer.prototype.toString = function toString() {
return this.buffer.join("");
};
StringBuffer.prototype.length = function length() {
return this.buffer.length;
};
StringBuffer.prototype.clear = function clear() {
this.buffer.splice(0,this.length());
return this;
};




(Post a new comment)


[info]lesoto
2006-08-21 12:20 pm UTC (link)
ты сделал copy-paste в неправильное окно :)

(Reply to this) (Thread)


[info]princeska
2006-08-21 12:40 pm UTC (link)
да уж, я чё-то непоал к чему это?.. :)

(Reply to this) (Parent)


[info]arhip
2006-08-21 02:15 pm UTC (link)
это себе на память и понимающим людям в помощь :) при работе с длинными стрингами повышает скорость в несколько раз..

(Reply to this) (Parent)


[info]ozlev
2006-08-23 05:00 am UTC (link)
Где appendFormat?
Самому допИсивать? - вот завсегда так...

(Reply to this) (Thread)


[info]arhip
2006-08-23 07:10 am UTC (link)
дык тут самое важное это концепт ;)
что push и join намного эффективнее, а остальное уже - роскошь :)

(Reply to this) (Parent)(Thread)


[info]ozlev
2006-08-23 07:15 am UTC (link)
push и join завсегда были эфективнее чем просто +=

(Reply to this) (Parent)(Thread)


[info]arhip
2006-08-23 07:21 am UTC (link)
ну да, но как то раньше в голову не приходило упаковать это в обьект :)

(Reply to this) (Parent)(Thread)


[info]ozlev
2006-08-23 07:31 am UTC (link)
Atlas спасёт Францию...
Тамочки и Format тудыже запаковали...

(Reply to this) (Parent)

OT DeHa
(Anonymous)
2006-08-25 01:50 pm UTC (link)
Bro , it is a really old stuff...
Wake up Neo....

(Reply to this)


Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…