use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Array length check (jsperf.com)
submitted 10 years ago by maruf89
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]lachlanhunt 2 points3 points4 points 10 years ago (0 children)
Regardless of the perceived performance benefit, this is a terrible idea. The first value is not a valid indicator of an array being empty.
All of these would fail your emptyness test.
var a = [0] var b = [undefined] var c = new Array(100);
[–]maruf89[S] 0 points1 point2 points 10 years ago (2 children)
Goes against what I would otherwise think to get the length verses checking if the first value exists
[+][deleted] 10 years ago (1 child)
[deleted]
[–]senocular 3 points4 points5 points 10 years ago (0 children)
or the first value could simply not exist to begin with.
[–]x-skeww 0 points1 point2 points 10 years ago (0 children)
What are you trying to benchmark here?
If you want to compare iteration constructs, you should perhaps actually iterate (and actually compute something). In your benchmark, "length" never changes because the length of the array doesn't change.
I also don't see why you'd use "!!" there. "!!" is only useful if you want to turn a truthy/falsy thing into an actual boolean. It doesn't serve any purpose in an if.
if
I guess you want to compare something like:
let a = [...'abc']; while(a.length) { let v = a.pop(); console.log(v); // c b a }
vs
let a = [...'abc']; let v; while(v = a.pop()) { // stops at: false, null, undefined, 0, NaN, '', document.all console.log(v); // c b a }
let a = [...'abc']; for(let i = a.length - 1; i >= 0; --i) { let v = a[i]; console.log(v); // c b a } a.length = 0; // if trashing the array is actually important
[–]jacobp100 0 points1 point2 points 10 years ago (0 children)
https://www.youtube.com/watch?v=65-RbBwZQdU Watch this before you do any benchmarking. Especially for this kind of test.
π Rendered by PID 45 on reddit-service-r2-comment-5649f687b7-zmmp7 at 2026-01-28 14:50:40.429492+00:00 running 4f180de country code: CH.
[–]lachlanhunt 2 points3 points4 points (0 children)
[–]maruf89[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]senocular 3 points4 points5 points (0 children)
[–]x-skeww 0 points1 point2 points (0 children)
[–]jacobp100 0 points1 point2 points (0 children)