Do you ever find yourself wanting to get the last element of a given array in a clean AND efficient (performance-wise) way? Well, look no more, as ...
For further actions, you may consider blocking this person and/or reporting abuse
How is
array[-1]
not faster than this?This obiously must be a joke post, look at the numbers.
Lol took me a while to understand πΉ
Blaze it!
That does not exist in javascript
or array[array.length - 1]
Lol ππ i forgot that
array[-1]
doesn't work, this answer of yours was what I meant but forgot to write the correct syntaxYou can use .at(-1) these days.
I think this is a better way (it took me a lot of work):
If anyone likes it you can request me for some more. All that hard work π
And of course, the less efficient but nice one-liner:
refact: simplify to 5 steps
Genius
I think you should set this up as a web service
Needs a graphql API, too, with a jwt authentication service.
It looks good but I think you can replace
const one = 69 * 420 - 69 * 419 - 34 * 2;
withconst one = 2 - 1;
maybe. A little bit more concise.What about
[1,2,3,4,5,6,7].slice(-1) // 7
?no that result is one element array : [7]
While this is very funny please remove the beginners and tutorial tags, a new developer may not get the joke and think that this is indeed a fast way to find the last item in an array.
Epicly terrible and absolutely inefficient way. Please tell me you're joking.
No, I am completely serious.
For...of loops are proven to perform way slower than
for(var i = 0; i < x; i++)
normal for loops and for you to perform that operation thrice, it's gonna slow down your code dramatically.Okay forget it, I literally am just not gonna talk to an absolute imbecile here. You wanna do it this way, then do it.
This
β β¦ += 1 - 1 + 1 - 2 + 5 - 3 β¦β
LOL π
array[array.length-1]
arr.at(-1)
const arr = [1, 2, 3, 4]
console.log(arr[arr.length - 1])
Lol! "THRICE"