Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
evilfunkyogi
Jun 27, 2005
:effort:
You shouldn't be prematurely optimizing. If your code has performance issues and profiling proves out that replacing all for..of loops with for loops fixes the problems then by all means do so in that project.

That doesn't mean you should never use for..of loops or go around writing reverse loops everywhere "because it's faster".

Adbot
ADBOT LOVES YOU

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Suspicious Dish posted:

I'm not joking when I say that in several of my projects, I replaced for(let foo of bar) with for(let i = 0; i < bar.length; i++) let foo = bar[i]; to a significant performance boost. This was after I profiled and saw it was an issue, and then fixed it.

This stuff matters, it really does. The compiler can often do a really crap job with some of this stuff.

Even faster calculate the length ahead of time

code:
const j = bar.length;
for(let i = 0; i < j; i++) {
    const foo = bar[i];
}
But true, for..of is notoriously slow. I cannot get enough of it.

huhu
Feb 24, 2006

Chas McGill posted:

Hmm I think I need a new host. Cheers for the advice.

I'm confused, do you not have access to a backend? How are you serving your code? Do you have a DB?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Joda posted:

Wait, really? You'd think the array iterable if anything would be optimised in the engine

Yeah, you'd think. But for...of isn't what it appears to be.

In this case, the slowdown was provided by GC pauses because I was using for...of every frame in a 60fps context. for...of allocates significantly more junk because of how the iterable protocol works. Creates a ton of garbage every iteration.

I still use for...of in non-every-frame code like init code.

Osmosisch
Sep 9, 2007

I shall make everyone look like me! Then when they trick each other, they will say "oh that Coyote, he is the smartest one, he can even trick the great Coyote."



Grimey Drawer

Nolgthorn posted:

Met somebody once, I was conducting their interview, and they wrote all of their loops in reverse.

code:
for (let i = 10; i >= 0; i--)
So I asked him why he was doing that, he said it was more performant.

Anyone who claims to care about performance that much and then uses postfix in/decrement is just weird to me.

The widespread use of postfix operators where the cached value is irrelevant is just so irritating to me (and yeah I know it will usually get compiled into prefix but ugh. Write what you mean).

smackfu
Jun 7, 2004

Nolgthorn posted:

Even faster calculate the length ahead of time

code:
const j = bar.length;
for(let i = 0; i < j; i++) {
    const foo = bar[i];
}
But true, for..of is notoriously slow. I cannot get enough of it.

Reminds me of code from 20 years ago.

(Unless length is actually calculated on demand, doubtful this is an improvement at all.)

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

smackfu posted:

(Unless length is actually calculated on demand, doubtful this is an improvement at all.)

It probably is done each iteration, unless the engine goes through the loop body to ensure that the array doesn't change length, which is not an easy problem to solve, and there are cases when it is outright unsolvable

necrotic
Aug 2, 2005
I owe my brother big time for this!
Is length not a stored value that only changes when you use array mutation methods? Why would it have to do anything except read a value?

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

necrotic posted:

Is length not a stored value that only changes when you use array mutation methods? Why would it have to do anything except read a value?

Yes, but how will you make absolutely sure that the array is never mutated? At what point do you stop resolving code paths and how do you know that someone isn't doing some crazy reflection poo poo somewhere or something. What if we get proprer threading. There's simply no reliable way to implement this other than taking the programmer intent at face value, and getting length each iteration.

Joda fucked around with this message at 16:04 on Dec 3, 2018

necrotic
Aug 2, 2005
I owe my brother big time for this!
No, I mean literally the value at array.length. Not some code analysis.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

necrotic posted:

No, I mean literally the value at array.length. Not some code analysis.
I think the point being made is it probably *is* a very small speed improvement, because putting the length in a local variable is one lookup per iteration, whereas getting it out of array.length is two (one for the location of the array and another for the location of the array's length property). Perhaps even worse if array.length goes through a getter function. Sure, using array.length each iteration isn't making the loop O(n^2) or anything, but in a non-compiled language using a plain variable is almost certainly [very slightly] faster than using a member property.

necrotic
Aug 2, 2005
I owe my brother big time for this!

roomforthetuna posted:

I think the point being made is it probably *is* a very small speed improvement, because putting the length in a local variable is one lookup per iteration, whereas getting it out of array.length is two (one for the location of the array and another for the location of the array's length property). Perhaps even worse if array.length goes through a getter function. Sure, using array.length each iteration isn't making the loop O(n^2) or anything, but in a non-compiled language using a plain variable is almost certainly [very slightly] faster than using a member property.

I was responding to Joda, who implied the length is computed every time you call array.length.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



necrotic posted:

I was responding to Joda, who implied the length is computed every time you call array.length.

I'm guessing Joda meant computed as in looked up/resolved, which it is as roomforthetuna notes. Saving the array length to a local is a pretty well-known optimization.

Joda posted:

Yes, but how will you make absolutely sure that the array is never mutated? At what point do you stop resolving code paths and how do you know that someone isn't doing some crazy reflection poo poo somewhere or something. What if we get proprer threading. There's simply no reliable way to implement this other than taking the programmer intent at face value, and getting length each iteration.

Throw an exception like DOM function NodeLists do :v:

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
No, not computed, just accessed. There'd be no reason to count elements every iteration. I don't think anyone is claiming that's the extent of the performance improvement. I'm literally talking a single instruction per iteration.

necrotic
Aug 2, 2005
I owe my brother big time for this!
Okay, I misunderstood you then.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
If you assign it to a local, would that most likely get compiled to some CPU register value that the next lines can access? If you just let it do "array.length", that would probably compile to a single address rather than an indirection, right? I assume that address would go to somewhere cached nicely and maybe even directly get to stay a register throughout the loop too

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

Dumb Lowtax posted:

If you assign it to a local, would that most likely get compiled to some CPU register value that the next lines can access? If you just let it do "array.length", that would probably compile to a single address rather than an indirection, right? I assume that address would go to somewhere cached nicely and maybe even directly get to stay a register throughout the loop too

It definitely won't get a register, and there's no guarantee it will receive a permanent space in the cache (L1 cache is basically as fast as registers these days, though.) That all depends on whether or not something aliases with it. The problem is that the value will either need to be refreshed in the cache on read, or on update it will have to be committed to memory, and how these operations are optimised and stuff vary between CPU vendors. It's much less complicated with a limited scope variable that the compiler knows can only be mutated in the immediate scope (E: Or better yet, a const that it knows it can make an immediate.)

Obviously, you're not going to be getting much performance, and we're at the level of optimisations that'll be absolutely last in line (if you bother with them at all)

Joda fucked around with this message at 21:53 on Dec 3, 2018

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop

Joda posted:

That all depends on whether or not something aliases with it.

What's an example of this?

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

Dumb Lowtax posted:

What's an example of this?

Say you have something at address 0xABCD and you have a cache that can hold 256 values from memory. You'd index by the last 8 bits (0xCD in this case,) then if you access 0xBBCD it aliases with the first value and it has to override it in the cache. That's obviously SUUUUPER simplified, but that's the general gist.

Doom Mathematic
Sep 2, 2008

Osmosisch posted:

Anyone who claims to care about performance that much and then uses postfix in/decrement is just weird to me.

The widespread use of postfix operators where the cached value is irrelevant is just so irritating to me (and yeah I know it will usually get compiled into prefix but ugh. Write what you mean).

Are you saying prefix increment/decrement is faster than postfix increment/decrement in that context?

Osmosisch
Sep 9, 2007

I shall make everyone look like me! Then when they trick each other, they will say "oh that Coyote, he is the smartest one, he can even trick the great Coyote."



Grimey Drawer

Doom Mathematic posted:

Are you saying prefix increment/decrement is faster than postfix increment/decrement in that context?

Probably not in that context, as I tried to explain with the bit about the compiler. It's just that I'd expect someone who is being anal about performance to not risk it.

Doom Mathematic
Sep 2, 2008

Osmosisch posted:

Probably not in that context, as I tried to explain with the bit about the compiler. It's just that I'd expect someone who is being anal about performance to not risk it.

Is there any context where there's a difference in performance, though?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Doom Mathematic posted:

Is there any context where there's a difference in performance, though?

i++ has* to copy i first so that it can subsequently do something with the old value. ++i doesn't need to copy, it can just do the increment and then do something with the new value. So maybe it matters if you care about literally every single cycle but are also not using an optimizing compiler.

*modulo optimizations

It's more plausibly relevant in C++, where both the ++ing and the thing you're doing with the result could be arbitrary method calls into some unknown library. But more relevantly, it never hurts performance, so it's exactly the sort of thing you'd expect someone cargo-culting microoptimizations without ever benchmarking anything to just do as a matter of course.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



After doing i++ for years just because that's how all the examples I learned from do it, I thought about it for a minute and realized that ++i is the operation I actually wanted/needed, so started doing it that way instead. Like if you have two equivalent functions that mutate a thing but one returns a value that you just don't use, I stop and wonder why.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Munkeymon posted:

After doing i++ for years just because that's how all the examples I learned from do it, I thought about it for a minute and realized that ++i is the operation I actually wanted/needed, so started doing it that way instead. Like if you have two equivalent functions that mutate a thing but one returns a value that you just don't use, I stop and wonder why.
I assume the prevalence of i++ is because ++i doesn't follow the pattern of almost every other mutation operation, leading with the thing that is to be mutated.
i = 5;
i += 5;
i.update();
i = someSortOfOperation(i);

The only other thing that comes to mind where the mutated thing is at the tail is functions with output parameters, which tended to be a workaround for functions only returning one thing, which is increasingly not true these days (C++ returning tuples, Javascript returning objects or arrays and deconstructing at the call site, etc.)

Chas McGill
Oct 29, 2010

loves Fat Philippe

huhu posted:

I'm confused, do you not have access to a backend? How are you serving your code? Do you have a DB?
No DB, it's a lovely starter hosting package that I could have PHP or WP stuff on, but I'd prefer to do something else if I need a backend.

Ranzear
Jul 25, 2013

A $5 linode can be ridiculously good and still scalable if you can handle linux.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop

roomforthetuna posted:

I assume the prevalence of i++ is because ++i doesn't follow the pattern of almost every other mutation operation, leading with the thing that is to be mutated.

THANK YOU this perfectly captures why it looks better to me. I really, really doubt I've ever coded anything in JS where performance suffered from this even if the compiler isn't just doing the same thing either way.

tracecomplete
Feb 26, 2017

I swore off web development for a long time, but then I ended up hands-on with React in ES6 and went "hey, this isn't bad!". I'm on the job market at the moment and for the first time in forever I'm considering jobs that involve touching a browser. Then I ended up using TypeScript and went "poo poo, why have I not always been using this?". Since then I've shipped a React Native app on the Google Play store and I've written plenty of React, and it's fine. I'd touch those webs.

But the overwhelming majority of jobs are JavaScript. ES6, if I'm lucky. ES6 is fine-for-a-dynamically-typed-language, I guess, but I found myself twice as fast at writing safe code when I had even the optional type checking of TypeScript (running in strict mode). To me it feels like a no-brainer if you're not going to go full suspenders on ReasonML or something.

Folks who write JavaScript rather than TypeScript (Flow, Elm, ReasonML people--you're excused)...why? Inertia/legacy? Employer won't do it? A dislike of it? To me it feels like moving backwards, but it's shaping up as though I'm going to have to end up somewhere where that's the norm again and I'd really like to understand the mindset involved.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop

I write short programs and I know what's in them, and never lose track of data types (and can diagnose when one is not what I think easily enough by hovering the mouse over it with Chrome Devtools). Thus I code in plain framework-free es6 because it's right there and there's fewer total rules to think about

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I write TypeScript because I find it worth the hassle, but 99% of these stupid bundler or compiler things are the most fragile pieces of software I've ever used, and manage not only to screw over your computer, but often browser devtools as well.

I'm currently using Parcel. It's the least bad of these stupid bundler things that I've used and it's still basically 100% busted.

I don't understand why anybody would willingly use Babel, for instance.

huhu
Feb 24, 2006
We use Flow.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

Suspicious Dish posted:

I don't understand why anybody would willingly use Babel, for instance.

I really don't like Babel. An alternative is Buble, it has always made me happy. Typescript has a really cool compiler, makes great code output. But for some reason it doesn't allow you to compile directly to iife, at least not the last time I looked. So I used to combine it with a second build step using rollup, that worked really well.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
I use babel literally only because it was the only way I could make TSX files work with web pack. And I use we pack because Chrome/WebStorm couldn't figure out source maps for my TS + React build in any other way.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

AFashionableHat posted:



Folks who write JavaScript rather than TypeScript (Flow, Elm, ReasonML people--you're excused)...why? Inertia/legacy? Employer won't do it? A dislike of it? To me it feels like moving backwards, but it's shaping up as though I'm going to have to end up somewhere where that's the norm again and I'd really like to understand the mindset involved.

"I have 3 days to write this, and I don't think learning typescript and getting the tooling all set up will fit in that."

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Lumpy posted:

"I have 3 days to write this, and I don't think learning typescript and getting the tooling all set up will fit in that."
This is good estimating.

lunar detritus
May 6, 2009


I have become so used to working with typescript that sometimes I struggle to remember method names when working in plain javascript because the autocompleting is amazing. :v:

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Suspicious Dish posted:

I write TypeScript because I find it worth the hassle, but 99% of these stupid bundler or compiler things are the most fragile pieces of software I've ever used, and manage not only to screw over your computer, but often browser devtools as well.

I'm currently using Parcel. It's the least bad of these stupid bundler things that I've used and it's still basically 100% busted.

I don't understand why anybody would willingly use Babel, for instance.

Babel I've used because I needed to get angular to work in IE11. I imagine there are other ways. I did get my most recent project to the point where source maps were working and was able to debug in typescript yet have it serve bundled files last time I made a project - however, I think if you have a project where you bundle/minify and you're not getting working source maps for whatever reason, either ditch the bundler if it doesn't support source maps at all, or fix the configuration.

I've seen people cling to using bundlers that don't emit source maps (like the one build into asp.net mvc, although apparently people have done projects to make it support it if you want to trust those?) and you can get 95% of the performance improvement just by enabling gzip compression for static files on the web server without having it gently caress your debugging.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

roomforthetuna posted:

This is good estimating.

What is "estimating"? Is that when you first hear the arbitrary deadline imposed on you?

Adbot
ADBOT LOVES YOU

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
A good estimation of how long typescript takes to setup

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply