1

Given the following code which works:

for (i=0; i<nLinears; i++) {
    for (j=0; j<nLinearPts[i]-1; j++) {
        $wb.upLinearLoad.append('<div>' + sprintf("%5s%8.1f to%7.1f%8.1f"
        ,sLinearSegName[i][j],fLinearPtBA[i][j],fLinearPtBA[i][j+1],fLen)
        .replace(/ /g,"&nbsp;"));
    }
}

This ensures that as the numbers change from small (more leading spaces) to large (fewer leading spaces), the column spacing will be maintained. However, as I understand it, using the regular expression for the .replace is not efficient, and as I have this kind of structure throughout the application, I need to have it run as fast as possible.

I believe that jQuery .text() will take care my need, but I also need to .append() the .text() result, and I can't figure out how to make them work together.

Any suggestions will be greatly appreciated.

Terry
  • 1,437
  • 2
  • 12
  • 26
  • You could update the `sprintf()` function to output non-breaking spaces in the first place. But have you actually found a performance problem using a regex `.replace()`? I don't think you'll have a problem. Given that you seem to have tabular data, why don't you use a ``? (By the way, I think you're missing a closing parenthesis before the semicolon.)
    – nnnnnn Dec 28 '11 at 01:52
  • Thanks for catching the missing paren. I had truncated the actual much longer statement for purposes of asking the question and accidentally took out the needed paren. I use tables in other places to take care of the problem, but I've heard that they too carry a high overhead. However, I'm not at all sure tables or the regex are a real performance problem. Later I'll probably try a number of things to speed it up, but for the moment I'm trying to get it done and only casually trying to keep it from being too slow. I think I'll take a quick look at the sprintf code. Thanks for the comment. – Terry Dec 28 '11 at 02:12
  • I looked at sprintf() and found that a simple one-statement change to substitute ' ' for the pad character insertion got me what I needed. – Terry Dec 29 '11 at 01:22
  • @MerlynMorgan-Graham - I've added my earlier comment as an answer, since that's what Terry seems to have gone with. Your suggestion is good though, given that any padding-based formatting is going to rely on a fixed-width font anyway. – nnnnnn Dec 29 '11 at 02:20
  • Done, and thanks for the info. Obviously I'm still feeling my way around StackOverflow. – Terry Dec 29 '11 at 06:10

3 Answers3

0

You could put these in <pre> elements instead of string-replacing &nbsp; repeatedly.

This would (usually) format them with a fixed-width font and preserve spaces without any extra work. The <pre> is for preformatted text, which makes sense for what you have here.

If this doesn't work perfectly and you need alternatives, you could check out this question about how to use pre-formatted text in different contexts.

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
0

You could update the sprintf() function to output non-breaking spaces in the first place.

But have you actually found a performance problem using a regex .replace()? I don't think you'll have a problem. Given that you seem to have tabular data, why don't you use a <table>? This is what tables are for.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

I looked at sprintf() and found that a simple one-statement change produced what I needed:

pad_character = '&nbsp;';

My thanks to the author of the sprintf() implementation for using meaningful variable names.

Terry
  • 1,437
  • 2
  • 12
  • 26