Generating sequence numbers

P

prino

I need to generate sequence numbers for data and I wonder if there is
a way to do this fast. The code I'm using right now,

for (var i=1;i<=9;i++)
document.write (('&nbsp;000'+i+'00<br />');
for (var i=10;i<=99;i++)
document.write (('&nbsp;00'+i+'00<br />');
for (var i=100;i<=737;i++)
document.write (('&nbsp;0'+i+'00<br />');

or

seq = 737
if (seq <= 10000)
{trail = '00'}
else
{trail = '0'}
for (var i = 1; i <= seq; i++)
{
s = '0000' + i + trail
document.write (('&nbsp;'+s.substr(s.length - 6, 6)+'<br />'));
}

are sooo slow. (The second caters for seqnos > 10000, which only have
one trailing zero)

Thanks,

Robert
 
E

Evertjan.

prino wrote on 11 sep 2007 in comp.lang.javascript:
I need to generate sequence numbers for data and I wonder if there is
a way to do this fast. The code I'm using right now,

for (var i=1;i<=9;i++)
document.write (('&nbsp;000'+i+'00<br />');
for (var i=10;i<=99;i++)
document.write (('&nbsp;00'+i+'00<br />');
for (var i=100;i<=737;i++)
document.write (('&nbsp;0'+i+'00<br />');

or [..]

are sooo slow. (The second caters for seqnos > 10000, which only have
one trailing zero)

You are not generating numbers but strings.
Avoid repeated document.write()s.
Using a single document.write() seems 4 times faster:
Even the dreaded repeated-string-concatenation
seems not to have that impact.

==================================
<script type='text/javascript'>

var t = +new Date()

var a = [];
var z = '00<br>';
for (var i=0;i<=736;i++)
a = (i+10001).toString().substr(1);
document.write(a.join(z)+z);

document.write(+new Date() - t + ' miliseconds');
// 16 miliseconds on my machine

</script>
===================================
<script type='text/javascript'>

var t = +new Date()

var a = [];
var z = '00<br>';
for (var i=0;i<=736;i++)
a = (i+10001).toString().substr(1)+z;
document.write(a.join(''));

document.write(+new Date() - t + ' miliseconds');
// same 16 miliseconds

</script>
====================================
<script type='text/javascript'>

var t = +new Date()

var r = '';
var z = '00<br>';
for (var i=0;i<=736;i++)
r += (i+10001).toString().substr(1)+z;
document.write(r);

document.write(+new Date() - t + ' miliseconds');
// same 16 miliseconds

</script>
=====================================
<script type='text/javascript'>

var t = +new Date()

var r = '';
var z = '00<br>';
for (var i=0;i<=736;i++)
document.write((i+10001).toString().substr(1)+z);

document.write(+new Date() - t + ' miliseconds');
// 63 miliseconds !!!!

</script>
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
I need to generate sequence numbers for data and I wonder if there is
a way to do this fast. The code I'm using right now,

Fancy meeting you here!

Try generating numbers starting at 1001, then stripping the first digit,
if you want numbers 001 to 737

If you can get away with counting down to 0, a while loop is faster than
your for loop. But the most time is probably in string handling. This
might be fast :

S = []
J = 1000
while (J++ < 1737) S.push(String(J*100).substr(1,6))
document.write(S)

but I might not have understood the requirements exactly.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top