OK, you beat me this time! :D You're about 3x faster
Swapping the toString()s for two comparisons speeds it up a bit, but it still takes more than double the amount of time yours does.
toString()
Removing the call to swap (but still swapping) makes them pretty much identical. I had no idea a function call could be so heavy.
swap
function put0AtTheBack(arr) { let zeroIndex = -1; for(let i = 0; i < arr.length; i++) { let item = arr[i]; if(item !== '0' && item !== 0 && zeroIndex !== -1) { let tmp = arr[i]; arr[i] = arr[zeroIndex]; arr[zeroIndex] = tmp; zeroIndex++; } if((item === '0' || item === 0) && zeroIndex === -1) { zeroIndex = i; } } }
That's an interesting find, I also wasn't aware that a function call would have made it that much worse.
Well, to be fair, that's over 100000 iterations. But surprising none-the-less.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
OK, you beat me this time! :D You're about 3x faster
Swapping the
toString()
s for two comparisons speeds it up a bit, but it still takes more than double the amount of time yours does.Removing the call to
swap
(but still swapping) makes them pretty much identical. I had no idea a function call could be so heavy.That's an interesting find, I also wasn't aware that a function call would have made it that much worse.
Well, to be fair, that's over 100000 iterations. But surprising none-the-less.