On Oct 19, 10:58 am, RobG <rg...@iinet.net.au> wrote:
> On Oct 15, 6:25 am, Jess Jacobs <simulacran.h...@gmail.com> wrote:
>
> > Hello everyone,
>
> > I ran into an interesting issue while trying to prove that adding string
> > concats to a long running string simply to fit the "80 char/line" idea was
> > not a good thing.
>
> >http://jsperf.com/string-concat-vs-long-lines
>
> > I discovered, unless my tests have errors (please call 'em out if so!):
>
> This seems to be an incredible waste of time. If your goal is to see
> if string concatenation in source code is an issue, it makes sense to
> first find out how prevalent it is. If only say 1% of lines are
> wrapped, and of those only 10% involve string concatenation, you need
> a really huge source code file to have a significant number of
> superfluous concatenations (e.g. 100,000 lines of code may only have
> 100 concatenations).


The following code builds lines of code and runs them through eval. In
Firefox and IE, the more concatentations, the slower it goes. The big
difference is that in IE, many concatenations of short strings is very
much slower than  a few concatenations of long strings.

But even for IE, eval'ing 1,000 lines of code of 300 strings of 2
words each took about 600ms for me. Doing the same with 1,000 lines
with 2 strings of 300 words each took about 100ms. Chrome took about
2,400ms and 120ms respectively.

So you need a very large number of very fragmented strings to get
significant differences.

var data = ('Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Cras sodales, ' +
           'nibh interdum cursus fringilla, massa lacus fermentum
ipsum, eget vehicula ' +
           'orci dui vel mi. Sed et imperdiet arcu. Proin non mi vitae
sem placerat ' +
           'volutpat. Nulla non lectus tortor. Sed dolor odio,
accumsan in laoreet vel, ' +
           'elementum vitae quam. Nunc in nisi tellus. Phasellus
ornare elementum dolor, ' +
           'a sollicitudin quam mollis ac. Aenean libero mi, sagittis
sed adipiscing a, ' +
           'eleifend at lorem. Integer id lorem leo, vitae posuere
libero. Proin in felis ' +
           'vitae lacus malesuada convallis. Donec ligula dolor,
molestie vel egestas eget, ' +
           'varius eget dui. Donec pharetra dapibus
condimentum.').split(' ');

// Build an array of expressions to create num statements
// that have numStrings strings and numWords words per string.
function buildLines(num, numStrings, numWords) {
  var lines = [];
  var len = data.length;
  var vv, code, tt;

  // For each line of code...
  for (var ii = 0; ii < num; ii++) {

    // Randomise variable name
    vv = 'abcdefghijklmnopqrstuvwxyz'.charAt(Math.random() * 26 | 0);
    code = 'var ' + vv + ' = ';

    // Build strings
    for (var jj = 0; jj < numStrings; jj++) {
      code += '"';
      tt = [];

      // Add words to string
      for (var kk = 0; kk < numWords; kk++) {
        tt.push(data[Math.random() * len | 0]);
      }
      // Add words to line of code as string
      code += tt.join(' ') + '"' + ((jj+1 < numStrings)? ' + ' : ';');
    }

    // Store line of code
    lines.push(code);
  }
  return lines;
}

function runCode(lines) {
  for (var ii=0, iLen=lines.length; ii<iLen; ii++) {
    eval(lines[ii]);
  }
}

var code, start, time;
var lines = 1000;
var xx = 300;
var yy = 2;

code = buildLines(lines, xx, yy);
start = new Date();
runCode(code);
time = (new Date() - start);
alert(lines + ' lines of ' + xx + ' strings and ' + yy + ' words per
string: ' + time);

code = buildLines(lines, yy, xx);
start = new Date();
runCode(code);
time = (new Date() - start);
alert(lines + ' lines of ' + yy + ' strings and ' + xx + ' words per
string: ' + time);


--
Rob

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to