Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #20408 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2013-06-23 22:50 -0700 |
| Last post | 2013-06-25 10:52 +0700 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.javascript
Looping holding old copy of array jonas.thornvall@gmail.com - 2013-06-23 22:50 -0700
Re: Looping holding old copy of array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-06-24 09:44 +0200
Re: Looping holding old copy of array "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-06-24 16:45 -0700
Re: Looping holding old copy of array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-25 03:06 +0200
Re: Looping holding old copy of array Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-06-25 21:58 +0200
Re: Looping holding old copy of array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-25 23:03 +0200
Re: Looping holding old copy of array JJ <duh@nah.meh> - 2013-06-24 15:19 +0700
Re: Looping holding old copy of array jonas.thornvall@gmail.com - 2013-06-24 03:38 -0700
Re: Looping holding old copy of array JJ <duh@nah.meh> - 2013-06-25 10:52 +0700
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2013-06-23 22:50 -0700 |
| Subject | Looping holding old copy of array |
| Message-ID | <6870a10e-ea81-40d5-91f8-ce425152e543@googlegroups.com> |
I have a looping algorithm that must use least amount of work.
I am working on an array and that use a counter for loop behaviour (loop in a loop).
However when loops finished, i need a copy of the array containing not the last item but the item before the last.
Basicly i have two while loops
while x>y
while z>t
change Aarray
I want the content saved of the pre Aarray before loopend
Here is my thought either i use
while x>y
newArray = Aarray.slice();
while z>t
change Aarray
or i use
while x>y
for(i=0;i<Aarray.length;i++){
newCopy[i] = Aarray[i];
}
while z>t
change Aarray
I do not like to make a copy everytime, but is there away around it?
Which of the two suggested methods is faster?
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-06-24 09:44 +0200 |
| Message-ID | <XnsA1E963239D075eejj99@194.109.133.133> |
| In reply to | #20408 |
jonas.thornvall wrote on 24 jun 2013 in comp.lang.javascript:
> I have a looping algorithm that must use least amount of work.
> I am working on an array and that use a counter for loop behaviour
> (loop in a loop).
>
> However when loops finished, i need a copy of the array containing not
> the last item but the item before the last.
>
> Basicly i have two while loops
> while x>y
> while z>t
> change Aarray
>
> I want the content saved of the pre Aarray before loopend
>
>
> Here is my thought either i use
>
> while x>y
> newArray = Aarray.slice();
> while z>t
> change Aarray
>
> or i use
>
>
> while x>y
> for(i=0;i<Aarray.length;i++){
> newCopy[i] = Aarray[i];
> }
> while z>t
> change Aarray
>
> I do not like to make a copy everytime, but is there away around it?
> Which of the two suggested methods is faster?
Could you tell what you what to accomplish,
in stead of giving crooked scripting examples
of how you want to do something,
we [I] cannot understand.
And please, jonas.thornvall, use a signature.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2013-06-24 16:45 -0700 |
| Message-ID | <f9fdda94-c977-42f5-af79-89a5d2155a85@googlegroups.com> |
| In reply to | #20409 |
On Monday, June 24, 2013 2:44:44 AM UTC-5, Evertjan. wrote:
> jonas.thornvall wrote on 24 jun 2013 in comp.lang.javascript:
>
>
>
> > I have a looping algorithm that must use least amount of work.
>
> > I am working on an array and that use a counter for loop behaviour
>
> > (loop in a loop).
>
> >
>
> > However when loops finished, i need a copy of the array containing not
>
> > the last item but the item before the last.
>
> >
>
> > Basicly i have two while loops
>
> > while x>y
>
> > while z>t
>
> > change Aarray
>
> >
>
> > I want the content saved of the pre Aarray before loopend
>
> >
>
> >
>
> > Here is my thought either i use
>
> >
>
> > while x>y
>
> > newArray = Aarray.slice();
>
> > while z>t
>
> > change Aarray
>
> >
>
> > or i use
>
> >
>
> >
>
> > while x>y
>
> > for(i=0;i<Aarray.length;i++){
>
> > newCopy[i] = Aarray[i];
>
> > }
>
> > while z>t
>
> > change Aarray
>
> >
>
> > I do not like to make a copy everytime, but is there away around it?
>
> > Which of the two suggested methods is faster?
[...]
I can't see the original message for some reason...
But at any rate, I'd suggest using myArray.map(...)
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map>
Why the concern over general speed? How big is the array?
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-06-25 03:06 +0200 |
| Message-ID | <2496880.a6ciHIckR8@PointedEars.de> |
| In reply to | #20423 |
Michael Haufe (TNO) wrote:
> On Monday, June 24, 2013 2:44:44 AM UTC-5, Evertjan. wrote:
>> jonas.thornvall wrote on 24 jun 2013 in comp.lang.javascript:
>>
>>
>>
>> > I have a looping algorithm that must use least amount of work.
>>
>> > I am working on an array and that use a counter for loop behaviour
>>
>> > (loop in a loop).
>>
>> >
>>
>> > However when loops finished, i need a copy of the array containing not
>>
>> > the last item but the item before the last.
>>
>> >
>>
>> > Basicly i have two while loops
>>
>> > while x>y
>>
>> > while z>t
>>
>> > change Aarray
>>
>> >
>>
>> > I want the content saved of the pre Aarray before loopend
>>
>> >
>>
>> >
>>
>> > Here is my thought either i use
>>
>> >
>>
>> > while x>y
>>
>> > newArray = Aarray.slice();
>>
>> > while z>t
>>
>> > change Aarray
>>
>> >
>>
>> > or i use
>>
>> >
>>
>> >
>>
>> > while x>y
>>
>> > for(i=0;i<Aarray.length;i++){
Safer and more efficient:
for (var i = 0, len = Aarray.length; i < len; ++i)
{
Using identifiers starting with capital letters for variables that do not
hold references to constructors is unwise.
>> > newCopy[i] = Aarray[i];
>>
>> > }
>>
>> > while z>t
>>
>> > change Aarray
>>
>> >
>>
>> > I do not like to make a copy everytime, but is there away around it?
>>
>> > Which of the two suggested methods is faster?
>
> [...]
>
> I can't see the original message for some reason...
>
> But at any rate, I'd suggest using myArray.map(...)
>
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map>
Unwise here for several reasons. Array.prototype.slice() is the most
efficient method to make a shallow copy of an encapsulated array structure.
It can also be used to make a shallow copy of only a part of the array
structure, hence its name.
I find the original question to be too unclearly worded to suggest a correct
alternative. On top of that it is hardly readable thanks to lack of Pretty
Printing, and to borken Google Groups; obviously it gets worse with any
followup to it posted via Google Groups (full quote intended here to show
that).
Do not use Google Groups for posting (unless you know how to work around its
bugs, assuming that possibility still exists).
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2013-06-25 21:58 +0200 |
| Message-ID | <kqcsf3$82s$1@dont-email.me> |
| In reply to | #20424 |
On 2013-06-25 03:06, Thomas 'PointedEars' Lahn wrote:
[...]
> Safer and more efficient:
>
> for (var i = 0, len = Aarray.length; i < len; ++i)
> {
By the way of above. According to discussion here
http://stackoverflow.com/questions/6261953/do-modern-javascript-jiters-need-array-length-caching-in-loops
in your opinion property "length" still needs to be stored in variable
before loop? Or this can be omitted because modern browsers caching (?)
"length" property?
--
Cezary Tomczyk
http://www.ctomczyk.pl/
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-06-25 23:03 +0200 |
| Message-ID | <3009252.vL9dYu562Q@PointedEars.de> |
| In reply to | #20438 |
Cezary Tomczyk wrote:
> On 2013-06-25 03:06, Thomas 'PointedEars' Lahn wrote:
> [...]
>> Safer and more efficient:
>>
>> for (var i = 0, len = Aarray.length; i < len; ++i)
>> {
>
> By the way of above. According to discussion here
> http://stackoverflow.com/questions/6261953/do-modern-javascript-jiters-need-array-length-caching-in-loops
> in your opinion property "length" still needs to be stored in variable
> before loop? Or this can be omitted because modern browsers caching (?)
> "length" property?
One should never rely on what one cannot know. You cannot know the runtime
environments code once written will be exposed to. There is no good reason
to assume the value of the ”length” property should be automatically cached
for a loop in the first place as it could change by statements in the loop.
One *can* know instead that avoiding to access a property repeatedly whose
value does *not* change, in favor of a local variable that holds this value,
reduces code complexity. That has a chance – and it has been showed – to
increase runtime efficiency, IOW to be faster. So it is wise to do that.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | JJ <duh@nah.meh> |
|---|---|
| Date | 2013-06-24 15:19 +0700 |
| Message-ID | <xes4r9lujrjk.o8wrdbwo8ef9.dlg@40tude.net> |
| In reply to | #20408 |
On Sun, 23 Jun 2013 22:50:49 -0700 (PDT), jonas.thornvall@gmail.com wrote:
> I have a looping algorithm that must use least amount of work.
> I am working on an array and that use a counter for loop behaviour (loop in a loop).
>
> However when loops finished, i need a copy of the array containing not the last item but the item before the last.
>
> Basicly i have two while loops
> while x>y
> while z>t
> change Aarray
>
> I want the content saved of the pre Aarray before loopend
>
> Here is my thought either i use
>
> while x>y
> newArray = Aarray.slice();
> while z>t
> change Aarray
>
> or i use
>
> while x>y
> for(i=0;i<Aarray.length;i++){
> newCopy[i] = Aarray[i];
> }
> while z>t
> change Aarray
>
> I do not like to make a copy everytime, but is there away around it?
> Which of the two suggested methods is faster?
Save the array only when needed.
Assuming the original code is like this:
while (x > y) {
newArray = Aarray.slice();
while (z > t) {
change(Aarray);
z++;
}
x++;
}
Change it to:
newArray = Aarray.slice();
while (x > y) {
if ((x+1) > y) {
newArray = Aarray.slice();
}
while (z > t) {
change(Aarray);
z++;
}
x++;
}
The first array save will make sure that it'll be saved if the execution
never enters the "outer" loop.
The second array save will be executed only at the last "outer" loop
iteration.
If you use break anywhere in the "outer" loop, then you'll need to add a
conditional array save for that too.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2013-06-24 03:38 -0700 |
| Message-ID | <f33df78c-bb23-4cb7-b7eb-c7e3900cd12f@googlegroups.com> |
| In reply to | #20411 |
Den måndagen den 24:e juni 2013 kl. 10:19:47 UTC+2 skrev JJ:
> On Sun, 23 Jun 2013 22:50:49 -0700 (PDT), jonas.thornvall@gmail.com wrote:
>
> > I have a looping algorithm that must use least amount of work.
>
> > I am working on an array and that use a counter for loop behaviour (loop in a loop).
>
> >
>
> > However when loops finished, i need a copy of the array containing not the last item but the item before the last.
>
> >
>
> > Basicly i have two while loops
>
> > while x>y
>
> > while z>t
>
> > change Aarray
>
> >
>
> > I want the content saved of the pre Aarray before loopend
>
> >
>
> > Here is my thought either i use
>
> >
>
> > while x>y
>
> > newArray = Aarray.slice();
>
> > while z>t
>
> > change Aarray
>
> >
>
> > or i use
>
> >
>
> > while x>y
>
> > for(i=0;i<Aarray.length;i++){
>
> > newCopy[i] = Aarray[i];
>
> > }
>
> > while z>t
>
> > change Aarray
>
> >
>
> > I do not like to make a copy everytime, but is there away around it?
>
> > Which of the two suggested methods is faster?
>
>
>
> Save the array only when needed.
>
>
>
> Assuming the original code is like this:
>
>
>
> while (x > y) {
>
> newArray = Aarray.slice();
>
> while (z > t) {
>
> change(Aarray);
>
> z++;
>
> }
>
> x++;
>
> }
>
>
>
> Change it to:
>
>
>
> newArray = Aarray.slice();
>
> while (x > y) {
>
> if ((x+1) > y) {
>
> newArray = Aarray.slice();
>
> }
>
> while (z > t) {
>
> change(Aarray);
>
> z++;
>
> }
>
> x++;
>
> }
>
>
>
> The first array save will make sure that it'll be saved if the execution
>
> never enters the "outer" loop.
>
>
>
> The second array save will be executed only at the last "outer" loop
>
> iteration.
>
>
>
> If you use break anywhere in the "outer" loop, then you'll need to add a
>
> conditional array save for that too.
Thank you for your answer, of course it must save computation time doing a check in the outer loop, rather then create the array every time.
I have another related question i just read there is break in javascript but does it work with while, using your idea could i do something like
newArray = Aarray.slice();
loop1:
while (x > y) {
if ((x+1) > y) {
break loop1;
}
while (z > t) {
change(Aarray);
z++;
}
x++;
}
The whole package will run many times in a global loop, so there will be many if checks depending on input size of array
Is there no other way then if clause to specify that the inner loop will break earlier last time it is run?
JT
Thanks again.
[toc] | [prev] | [next] | [standalone]
| From | JJ <duh@nah.meh> |
|---|---|
| Date | 2013-06-25 10:52 +0700 |
| Message-ID | <1s2gb6d5qci6$.1hmc7za9siq9p.dlg@40tude.net> |
| In reply to | #20415 |
On Mon, 24 Jun 2013 03:38:20 -0700 (PDT), jonas.thornvall@gmail.com wrote:
> I have another related question i just read there is break in javascript but does it work with while, using your idea could i do something like
>
> newArray = Aarray.slice();
>
> loop1:
> while (x > y) {
> if ((x+1) > y) {
> break loop1;
> }
>
> while (z > t) {
> change(Aarray);
> z++;
> }
> x++;
> }
"break" works for any loops as well as for "switch". But your labelled
"break" usage there, will cause infinite loop when ((x+1) > y), because
"break" will simply go back to the start of outer loop without allowing x to
be incremented first.
The labelled "break" is best used in the nested loop.
newArray = Aarray.slice();
loop1:
while (x > y) {
while (z > t) {
if (allStop()) {
newArray = Aarray.slice();
x = y; //update x to fail the outer loop condition
break loop1;
}
change(Aarray);
z--;
}
x--;
}
> Is there no other way then if clause to specify that the inner loop will break earlier last time it is run?
All loops only has one condition check. What you need is a different
condition, so you'll need to add one manually.
A loop will break only if you put a "break" in it. Otherwise, the loop will
simply ends once the loop condition fails.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web