0

i have a problem, my Java Script runs out of memory and i dont know why. Memory usage jumps to almost 2gB when I start the code. Currently if i comment out this part of my code it works fine:

var way = [];
for(var x = 0;x<=way1.length > way2.length?way1.length:way2.length;x++){
    way.push(way1[x]);
    way.push(way2[x]);
}

otherwise it will crashe.

Thanks for help.

1
  • 1
    Without looking deeply in the code. Why the hell do you have such complex expression inside the for? That is probably your issue Commented Feb 25, 2018 at 8:55

3 Answers 3

3

The expression inside the loop does not do what you think it does.

x <= way1.length > way2.length? way1.length : way2.length

What you probably meant

x <= (way1.length > way2.length? way1.length : way2.length)

It actually does

(x <= way1.length > way2.length) ? way1.length : way2.length

Which always return the same number. Either way1.length or way2.length - which is always true. And the loops goes infinity.

Sign up to request clarification or add additional context in comments.

Comments

1

Your condition is very confusing, and is most probably producing a wrong result, maybe you should do

var way = [];
for(var x = 0, length = Math.max(way1.length, way2.length); x <= length; x++){
    way.push(way1[x]);
    way.push(way2[x]);
}

Comments

0

Simplify the loop condition first, I think condition making your program out of memory

1 Comment

Ok, i´ll try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.