Use strings accumulator-style?

Discussion in 'Javascript' started by Kamjah Kogumah, Dec 14, 2006.

  1. A Dev Opera article [1] suggest using code 2 instead of code 1 in
    performance-critical functions.

    code 1:
    a += 'x' + 'y';

    code 2:
    a += 'x';
    a += 'y';

    But I thought string concatenation takes longer time when involved
    strings are long.
    code 1 does two concatenations : 'x'+'y' and a+'xy'
    code 2 does : a+'x' and a+'y'.
    If a is long enough, code 2 should take twice much time as code 1
    because it takes small time to do 'x'+'y' and it takes long to do
    a+something. Am I missing something?

    [1] Dev Opera - Efficient JavaScript - Use strings accumulator-style
    http://dev.opera.com/articles/view/efficient-javascript/?page=2#stringaccumulator
     
    Kamjah Kogumah, Dec 14, 2006
    #1
    1. Advertising

  2. Kamjah Kogumah

    Erwin Moller Guest

    Kamjah Kogumah wrote:

    > A Dev Opera article [1] suggest using code 2 instead of code 1 in
    > performance-critical functions.
    >
    > code 1:
    > a += 'x' + 'y';
    >
    > code 2:
    > a += 'x';
    > a += 'y';
    >
    > But I thought string concatenation takes longer time when involved
    > strings are long.
    > code 1 does two concatenations : 'x'+'y' and a+'xy'
    > code 2 does : a+'x' and a+'y'.
    > If a is long enough, code 2 should take twice much time as code 1
    > because it takes small time to do 'x'+'y' and it takes long to do
    > a+something. Am I missing something?
    >
    > [1] Dev Opera - Efficient JavaScript - Use strings accumulator-style
    >

    http://dev.opera.com/articles/view/efficient-javascript/?page=2#stringaccumulator

    Why don't you do some profiling with long strings and just check the time
    needed to perform the operations? You can see for yourself if it is right
    what is claimed in the article.

    But I agree with you it doesn't make a lot of sense.
    Maybe some mechanism under the hood is optimized for the a += 'x' approach.
    (Guessing now)
    maybe 'x' + 'y' will be evaluated first into a new value, and then added to
    a, while the construct a += 'x' will simply add to the string without need
    to make a temporarely variable holding 'x'+'y'.

    In such rare cases I care about speed I alway profile in the poor man's way:
    timestamping.

    just my 2 cent

    Regards,
    Erwin Moller
     
    Erwin Moller, Dec 14, 2006
    #2
    1. Advertising

  3. Erwin Moller wrote:

    > Kamjah Kogumah wrote:
    >
    > > A Dev Opera article [1] suggest using code 2 instead of code 1 in
    > > performance-critical functions.
    > >
    > > code 1:
    > > a += 'x' + 'y';
    > >
    > > code 2:
    > > a += 'x';
    > > a += 'y';
    > >
    > > But I thought string concatenation takes longer time when involved
    > > strings are long.
    > > code 1 does two concatenations : 'x'+'y' and a+'xy'
    > > code 2 does : a+'x' and a+'y'.
    > > If a is long enough, code 2 should take twice much time as code 1
    > > because it takes small time to do 'x'+'y' and it takes long to do
    > > a+something. Am I missing something?
    > >
    > > [1] Dev Opera - Efficient JavaScript - Use strings accumulator-style
    > >

    > http://dev.opera.com/articles/view/efficient-javascript/?page=2#stringaccumulator
    >
    > Why don't you do some profiling with long strings and just check the time
    > needed to perform the operations? You can see for yourself if it is right
    > what is claimed in the article.
    >
    > But I agree with you it doesn't make a lot of sense.
    > Maybe some mechanism under the hood is optimized for the a += 'x' approach.
    > (Guessing now)
    > maybe 'x' + 'y' will be evaluated first into a new value, and then added to
    > a, while the construct a += 'x' will simply add to the string without need
    > to make a temporarely variable holding 'x'+'y'.
    >
    > In such rare cases I care about speed I alway profile in the poor man's way:
    > timestamping.
    >


    According to my profiling,
    code 2 takes twice much time as code 1 in Firefox 2.0.
    code 2 takes about 0.8 of time as code 1 in Opera 9. code 2 is faster
    in Opera.
    both codes takes about same time in Internet Explorer 6


    Tested with online Tryit Editor. Test source:

    <html><body><script type="text/javascript">

    times = {}; //global.
    function tick(id){
    id = id || 'def';
    times[id] = (new Date()).getTime();
    }
    function tack(id){
    id = id || 'def';
    times[id] = (new Date()).getTime() - times[id];
    return times[id];
    }
    function measure(f,message){
    tick();
    f();
    var t = tack();
    document.write(message+' : '+t+'<br>');
    }
    m = 10000;
    n = 100; //global
    measure( function(){
    var a ='';
    for(var j=0;j<m;j++){
    a = '';
    for(var i=0;i<n;i++){
    a += 'x' + 'y';
    }
    }
    },'temp var');
    measure( function(){
    var a ='';
    for(var j=0;j<m;j++){
    a = '';
    for(var i=0;i<n;i++){
    a += 'x';
    a += 'y';
    }
    }
    },'two concat');

    </script></body></html>


    test result:

    firefox 2.0
    first try
    temp var : 4157
    two concat : 9359
    second try
    temp var : 4094
    two concat : 8765

    opera 9
    first try
    temp var : 3266
    two concat : 2625
    second try
    temp var : 3328
    two concat : 2703

    ie 6 (with modified setting m = 5000)
    first try
    temp var : 3266
    two concat : 3297
    second try
    temp var : 3265
    two concat : 3297
     
    Kamjah Kogumah, Dec 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kamlesh
    Replies:
    0
    Views:
    966
    Kamlesh
    Apr 21, 2005
  2. Raymond Hettinger

    Pre-PEP: Dictionary accumulator methods

    Raymond Hettinger, Mar 19, 2005, in forum: Python
    Replies:
    125
    Views:
    2,053
    Steven Bethard
    Apr 1, 2005
  3. MariuszK
    Replies:
    0
    Views:
    726
    MariuszK
    Nov 11, 2007
  4. Cameron

    accumulator generators

    Cameron, May 30, 2008, in forum: Python
    Replies:
    6
    Views:
    1,257
    Arnaud Delobelle
    May 31, 2008
  5. Mike Treseler
    Replies:
    0
    Views:
    666
    Mike Treseler
    Feb 8, 2009
Loading...

Share This Page