Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Performance

You know exactly how long the resulting array should be, so don't make the interpreter guess. Any time the interpreter underestimates the length of an array and has to expand it, there's a significant performance penalty, as it reallocates a larger chunk of contiguous memory and transfers the already copied elements there.

var copyarray = new Array(arr.length);

That said, do you really need to copy the array? If not, then you should just swap the first k elements in place for best performance.

As for the looping, it doesn't much matter whether you write it as one loop or two loops. The running time will be dominated by the memory accesses: n reads and n writes.

Code hygiene

Did you really drop a vowel from the function name to save one byte? If so, you are repeating Ken Thompson's greatest regret Ken Thompson's greatest regret.

Although JavaScript allows you to omit semicolons at the end of each statement, it's considered bad practice to do so in all but the most trivial cases, such as <a onclick="alert('Hello')">Hello</a>.

It's odd to have a function that just logs its results. Either return copyarray (and let the caller call console.log()), or reverse the entries in place.

Performance

You know exactly how long the resulting array should be, so don't make the interpreter guess. Any time the interpreter underestimates the length of an array and has to expand it, there's a significant performance penalty, as it reallocates a larger chunk of contiguous memory and transfers the already copied elements there.

var copyarray = new Array(arr.length);

That said, do you really need to copy the array? If not, then you should just swap the first k elements in place for best performance.

As for the looping, it doesn't much matter whether you write it as one loop or two loops. The running time will be dominated by the memory accesses: n reads and n writes.

Code hygiene

Did you really drop a vowel from the function name to save one byte? If so, you are repeating Ken Thompson's greatest regret.

Although JavaScript allows you to omit semicolons at the end of each statement, it's considered bad practice to do so in all but the most trivial cases, such as <a onclick="alert('Hello')">Hello</a>.

It's odd to have a function that just logs its results. Either return copyarray (and let the caller call console.log()), or reverse the entries in place.

Performance

You know exactly how long the resulting array should be, so don't make the interpreter guess. Any time the interpreter underestimates the length of an array and has to expand it, there's a significant performance penalty, as it reallocates a larger chunk of contiguous memory and transfers the already copied elements there.

var copyarray = new Array(arr.length);

That said, do you really need to copy the array? If not, then you should just swap the first k elements in place for best performance.

As for the looping, it doesn't much matter whether you write it as one loop or two loops. The running time will be dominated by the memory accesses: n reads and n writes.

Code hygiene

Did you really drop a vowel from the function name to save one byte? If so, you are repeating Ken Thompson's greatest regret.

Although JavaScript allows you to omit semicolons at the end of each statement, it's considered bad practice to do so in all but the most trivial cases, such as <a onclick="alert('Hello')">Hello</a>.

It's odd to have a function that just logs its results. Either return copyarray (and let the caller call console.log()), or reverse the entries in place.

Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

Performance

You know exactly how long the resulting array should be, so don't make the interpreter guess. Any time the interpreter underestimates the length of an array and has to expand it, there's a significant performance penalty, as it reallocates a larger chunk of contiguous memory and transfers the already copied elements there.

var copyarray = new Array(arr.length);

That said, do you really need to copy the array? If not, then you should just swap the first k elements in place for best performance.

As for the looping, it doesn't much matter whether you write it as one loop or two loops. The running time will be dominated by the memory accesses: n reads and n writes.

Code hygiene

Did you really drop a vowel from the function name to save one byte? If so, you are repeating Ken Thompson's greatest regret.

Although JavaScript allows you to omit semicolons at the end of each statement, it's considered bad practice to do so in all but the most trivial cases, such as <a onclick="alert('Hello')">Hello</a>.

It's odd to have a function that just logs its results. Either return copyarray (and let the caller call console.log()), or reverse the entries in place.

default

AltStyle によって変換されたページ (->オリジナル) /