more efficient way to do String concat

172 views
Skip to first unread message

Wei Xue

unread,
Mar 13, 2012, 1:59:28 PM3/13/12
to mozill...@googlegroups.com
Some of our scripts have a lot of String concat which causes high GC in the server causing by allocating array.   I wonder in Rhino, is there equivalent of StringBuffer?  If I write my scrip in the following way, will it be efficient when running in Rhino?

var buf= [];
buf.push('some string'); 
buf.toString();

to replace following:

var buf='';
buf+='some string';

Thanks!

Michelle

Michael Schwartz

unread,
Mar 13, 2012, 2:46:17 PM3/13/12
to mozill...@googlegroups.com
Try:

var buf = [];
buf.push('some string');
buf.join('');

Johan Compagner

unread,
Mar 14, 2012, 8:37:17 AM3/14/12
to mozill...@googlegroups.com
We have a patched Rhino for this that when you do concatting the string that you concat is a string buffer as much as possible
So internally it is a string buffer and append will be used if you concat it will reuse it, and at the moment you use it as a string it will be converted.

But i think there are now also optimizes like this in the current trunk of rhino?

Hannes Wallnöfer

unread,
Mar 14, 2012, 8:53:41 AM3/14/12
to mozill...@googlegroups.com
Current Rhino git snapshot and 1.7R4 which is due very soon has
optimized String concatenation, so using + to build strings should be
comparable to array join() in terms of performance.

Hannes

2012/3/13 Wei Xue <wei....@gmail.com>:

Wei Xue

unread,
Mar 14, 2012, 11:06:13 AM3/14/12
to mozill...@googlegroups.com
Thanks all for your reply.  I'll use join for now until I switch to 1.7R4.

Michelle

2012/3/14 Hannes Wallnöfer <han...@gmail.com>

Wei Xue

unread,
Apr 3, 2012, 9:10:58 PM4/3/12
to mozill...@googlegroups.com
Hannes, what's the branch name for 1.7R4? Thanks. I don't see it in the following list.

Rhino1_7R3_RELEASE: 1.70
head: 1.62
Rhino1_7R2_BRANCH: 1.62.0.2
BEFORE_AST: 1.62
Rhino1_7R1_BRANCH: 1.59.0.2
Rhino1_6R7_BRANCH: 1.57.0.4
Rhino1_6R6_RELEASE: 1.57
Rhino1_6R6_BRANCH: 1.57.0.2
RHINO_2007_02_05_08_30: 1.1.1.1
MOZILLA: 1.1.1
Rhino1_6R5_RELEASE: 1.52.2.2
Rhino1_6R4_RELEASE: 1.52
Rhino1_6R3_PATCH: 1.52.0.2
JS_1_7_ALPHA_MERGE: 1.52
Rhino1_6R3_RELEASE: 1.52
SUNBIRD_0_3a2_RELEASE: 1.52
Rhino1_6R2_RELEASE: 1.51
PREFERENCES_20050201_BRANCH: 1.49.0.2
PREFERENCES_20050201_BASE: 1.49
before_simpler_undefined_280047: 1.49
Rhino1_6R1_RELEASE: 1.49
BEFORE_E4X: 1.44
Rhino150R5_RELEASE: 1.38
before_225831_merge: 1.37
templess_bytecode_225831: 1.37.0.2
before_less_classes_optimizer: 1.33
Rhino150R4_1_RELEASE: 1.29
Rhino150R4_BRANCH: 1.29.0.2
Rhino150R4_RELEASE: 1.29
less_static: 1.21.0.2
Rhino150R3_RELEASE: 1.20
EDITOR_EMBEDDING_20011025_BRANCH: 1.16.0.2
EDITOR_EMBEDDING_20011025_BASE: 1.16
Rhino150R2_RELEASE: 1.20
Rhino150R2_BRANCH: 1.20.0.2
before_new_natives: 1.11
before_jsdebugger: 1.9
EXP_DEBUGGER: 1.8.0.2
Rhino150R1_RELEASE: 1.8
Rhino150R1_BRANCH: 1.8.0.4
ClassGen_BRANCH: 1.4.0.2
Rhino140R3_BRANCH: 1.1.0.2
Rhino140R3_RELEASE: 1.1

2012/3/14 Hannes Wallnöfer <han...@gmail.com>

Hannes Wallnöfer

unread,
Apr 4, 2012, 5:21:47 AM4/4/12
to mozill...@googlegroups.com
1.7 is on the master branch.

Hannes

2012/4/4 Wei Xue <wei....@gmail.com>:

Reply all
Reply to author
Forward
0 new messages