Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. Typescript optimisation benchmarks

Typescript optimisation benchmarks

Scheduled Pinned Locked Moved The Lounge
javascriptcomalgorithmsperformancequestion
14 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Maunder
    wrote on last edited by
    #1

    Just came across GitHub - aminya/typescript-optimization: Compares different for-loops in TypeScript/JavaScript[^] and thought it was interesting. I still, after all these years, don't get why something like foreach or for...of should be any slower than for (...). I understand the generalisations and checks that have to happen, but in a typesafe language surely, once the compiler's worked things out, it can all just boil down to the fastest option at runtime regardless of syntax?

    cheers Chris Maunder

    P J L 3 Replies Last reply
    0
    • C Chris Maunder

      Just came across GitHub - aminya/typescript-optimization: Compares different for-loops in TypeScript/JavaScript[^] and thought it was interesting. I still, after all these years, don't get why something like foreach or for...of should be any slower than for (...). I understand the generalisations and checks that have to happen, but in a typesafe language surely, once the compiler's worked things out, it can all just boil down to the fastest option at runtime regardless of syntax?

      cheers Chris Maunder

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Not really. (At least in C#.) Polymorphism means the actual type isn't known until runtime, so it can't decide which technique is best. I say use for or while whenever you can and only use foreach as a last resort, when the others don't work. foreach has serious limitations, as do its proponents. It's often a case of the developer having more information about what's "best" than the compiler does. Do the compiler and optimizer a favor and pre-optimize.

      C G 2 Replies Last reply
      0
      • P PIEBALDconsult

        Not really. (At least in C#.) Polymorphism means the actual type isn't known until runtime, so it can't decide which technique is best. I say use for or while whenever you can and only use foreach as a last resort, when the others don't work. foreach has serious limitations, as do its proponents. It's often a case of the developer having more information about what's "best" than the compiler does. Do the compiler and optimizer a favor and pre-optimize.

        C Offline
        C Offline
        Chris Maunder
        wrote on last edited by
        #3

        I think about what happens with the LINQ Count() extension: if, at runtime, it's known the collection has a Count property then it'll just use that; otherwise it will iterate and actually count the items. That's the sort of thing I keep thinking should be happening with for loops. But I'm fairly sure very, very smart people have spent way too much time thinking about this so there's clearly an obvious and difficult issue stopping them.

        cheers Chris Maunder

        P 1 Reply Last reply
        0
        • P PIEBALDconsult

          Not really. (At least in C#.) Polymorphism means the actual type isn't known until runtime, so it can't decide which technique is best. I say use for or while whenever you can and only use foreach as a last resort, when the others don't work. foreach has serious limitations, as do its proponents. It's often a case of the developer having more information about what's "best" than the compiler does. Do the compiler and optimizer a favor and pre-optimize.

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          PIEBALDconsult wrote:

          I say use for or while whenever you can and only use foreach as a last resort, when the others don't work. foreach has serious limitations, as do its proponents.

          Obligatory xkcd: Optimization[^] My experience with foreach is from C# rather than TypeScript, but would think the same reasoning applies. For me the choice of foreach vs. for (...) is based on whether I care about the state of the iteration within the loop or not. If I don't, then foreach is preferable. If I do, then either for (...) or while (...) wins. The construct being iterated over plays a role as well. If it's a simple array or an array-like class, then for (...) gets a stronger weight in the voting. If the only way to iterate is through IEnumerable or something similar, then I'll almost always use foreach. The goal for the decision is to choose the simplest, most natural iteration method based on the needs at the time. As far as performance goes, I would think it doesn't matter in most cases. If you find a case where the enumeration technique dramatically affects performance, I'd be inclined to rethink the algorithm.

          Software Zen: delete this;

          P honey the codewitchH 2 Replies Last reply
          0
          • G Gary R Wheeler

            PIEBALDconsult wrote:

            I say use for or while whenever you can and only use foreach as a last resort, when the others don't work. foreach has serious limitations, as do its proponents.

            Obligatory xkcd: Optimization[^] My experience with foreach is from C# rather than TypeScript, but would think the same reasoning applies. For me the choice of foreach vs. for (...) is based on whether I care about the state of the iteration within the loop or not. If I don't, then foreach is preferable. If I do, then either for (...) or while (...) wins. The construct being iterated over plays a role as well. If it's a simple array or an array-like class, then for (...) gets a stronger weight in the voting. If the only way to iterate is through IEnumerable or something similar, then I'll almost always use foreach. The goal for the decision is to choose the simplest, most natural iteration method based on the needs at the time. As far as performance goes, I would think it doesn't matter in most cases. If you find a case where the enumeration technique dramatically affects performance, I'd be inclined to rethink the algorithm.

            Software Zen: delete this;

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I have studied some affects of it here: On why to use DataViews[^] In a few simple cases I do use foreach, but when performance is critical I do not use foreach, I am more likely to use GetEnumerator() and use multi-threading to iterate the items.

            G 1 Reply Last reply
            0
            • C Chris Maunder

              I think about what happens with the LINQ Count() extension: if, at runtime, it's known the collection has a Count property then it'll just use that; otherwise it will iterate and actually count the items. That's the sort of thing I keep thinking should be happening with for loops. But I'm fairly sure very, very smart people have spent way too much time thinking about this so there's clearly an obvious and difficult issue stopping them.

              cheers Chris Maunder

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              But I don't see how it can do that at compile time, it sounds like it has to use reflection to determine whether or not the provided class has a Count property. That is, (in C#) if the compiler only knows that the class will be IEnumerable or IEnumerable<T>, then it can't know at compile time what will be provided. Sure, if it knows that a List or Array will be provided, then it should be able to. But that means you know it is a List or Array, so use a better loop. And don't use Linq (ptui).

              C 1 Reply Last reply
              0
              • G Gary R Wheeler

                PIEBALDconsult wrote:

                I say use for or while whenever you can and only use foreach as a last resort, when the others don't work. foreach has serious limitations, as do its proponents.

                Obligatory xkcd: Optimization[^] My experience with foreach is from C# rather than TypeScript, but would think the same reasoning applies. For me the choice of foreach vs. for (...) is based on whether I care about the state of the iteration within the loop or not. If I don't, then foreach is preferable. If I do, then either for (...) or while (...) wins. The construct being iterated over plays a role as well. If it's a simple array or an array-like class, then for (...) gets a stronger weight in the voting. If the only way to iterate is through IEnumerable or something similar, then I'll almost always use foreach. The goal for the decision is to choose the simplest, most natural iteration method based on the needs at the time. As far as performance goes, I would think it doesn't matter in most cases. If you find a case where the enumeration technique dramatically affects performance, I'd be inclined to rethink the algorithm.

                Software Zen: delete this;

                honey the codewitchH Offline
                honey the codewitchH Offline
                honey the codewitch
                wrote on last edited by
                #7

                Sometimes heavy enumeration is just par for the course. When you're computing parsing tables for example, you have to do a lot of iteration over grammar constructs. It's unavoidable because it's baked into the math. If you found a way to do something like say, subset construction without doing as much iteration you could probably make a lot of money demonstrating that technique. One reason I tend to avoid foreach in C# these days is I port a lot of code to C++14 sans STL for use on IoT devices so foreach isn't really available, and is harder to translate. I know that's a special case, but it comes up a lot for me. :)

                Real programmers use butterflies

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  I have studied some affects of it here: On why to use DataViews[^] In a few simple cases I do use foreach, but when performance is critical I do not use foreach, I am more likely to use GetEnumerator() and use multi-threading to iterate the items.

                  G Offline
                  G Offline
                  Gary R Wheeler
                  wrote on last edited by
                  #8

                  PIEBALDconsult wrote:

                  use multi-threading to iterate

                  That is definitely one of the magic words for choosing something other than foreach. Come to think of it, I wouldn't be surprised to see a multithreaded foreach at some point. They've added all manner of kitchen sinks to C# in recent years, what's one more?

                  Software Zen: delete this;

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    But I don't see how it can do that at compile time, it sounds like it has to use reflection to determine whether or not the provided class has a Count property. That is, (in C#) if the compiler only knows that the class will be IEnumerable or IEnumerable<T>, then it can't know at compile time what will be provided. Sure, if it knows that a List or Array will be provided, then it should be able to. But that means you know it is a List or Array, so use a better loop. And don't use Linq (ptui).

                    C Offline
                    C Offline
                    Chris Maunder
                    wrote on last edited by
                    #9

                    PIEBALDconsult wrote:

                    But I don't see how it can do that at compile time

                    There will be some cases when it can at compile time:

                    let arr: number= [ 1,2,3 ];
                    for (let x of arr) {
                    ...
                    }

                    for this it's pretty clear cut. For other scenarios, some runtime pre-checks, then choose the fastest implementation that works. Anyway, I'm way out of my depth on this stuff but it does seem odd we're still having the "never use foreach" directives.

                    cheers Chris Maunder

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      Just came across GitHub - aminya/typescript-optimization: Compares different for-loops in TypeScript/JavaScript[^] and thought it was interesting. I still, after all these years, don't get why something like foreach or for...of should be any slower than for (...). I understand the generalisations and checks that have to happen, but in a typesafe language surely, once the compiler's worked things out, it can all just boil down to the fastest option at runtime regardless of syntax?

                      cheers Chris Maunder

                      J Offline
                      J Offline
                      Jacquers
                      wrote on last edited by
                      #10

                      No coding questions allowed in the lounge :P

                      B 1 Reply Last reply
                      0
                      • J Jacquers

                        No coding questions allowed in the lounge :P

                        B Offline
                        B Offline
                        BillWoodruff
                        wrote on last edited by
                        #11

                        Jacquers wrote:

                        No coding questions allowed in the lounge

                        That is an exhibit in the now abandoned Museum of Rules :wtf:

                        «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

                        theoldfoolT 1 Reply Last reply
                        0
                        • B BillWoodruff

                          Jacquers wrote:

                          No coding questions allowed in the lounge

                          That is an exhibit in the now abandoned Museum of Rules :wtf:

                          «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

                          theoldfoolT Offline
                          theoldfoolT Offline
                          theoldfool
                          wrote on last edited by
                          #12

                          I think these are more theoretical statements than coding questions. I find them interesting. I would also avoid pulling on superman's cape. :)

                          >64 If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.

                          B 1 Reply Last reply
                          0
                          • theoldfoolT theoldfool

                            I think these are more theoretical statements than coding questions. I find them interesting. I would also avoid pulling on superman's cape. :)

                            >64 If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.

                            B Offline
                            B Offline
                            BillWoodruff
                            wrote on last edited by
                            #13

                            @theoldfool @chris-maunder My comment was meant to be ironic, but, it has a context which involves my (unsuccessful) attempts, over years, to address what I see as the issue of useful technical content getting submerged in the tidal waves of Lounge activity. imho, a lot valuable content is not curated in a way that newer users or visitors are aware of it ... why would they sort through the usual banter, CC's, etc., to find content relevant to specific technical needs ? The solutions to this ... I can see ... are, unfortunately, impractical: having intelligent people curate takes either paying for employees; or, motivating a large number of members to pick up an oar. The "ideal" solution would be for posters to actually use the Forums that exist. But, then, the spotlight for folks like Honey Witch might not be bright enough :) Like I say, I've given up on this ... but, I still enjoy CodeProject as much as ever ! another Old Fool, Bill (ex-idealist)

                            «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

                            1 Reply Last reply
                            0
                            • C Chris Maunder

                              Just came across GitHub - aminya/typescript-optimization: Compares different for-loops in TypeScript/JavaScript[^] and thought it was interesting. I still, after all these years, don't get why something like foreach or for...of should be any slower than for (...). I understand the generalisations and checks that have to happen, but in a typesafe language surely, once the compiler's worked things out, it can all just boil down to the fastest option at runtime regardless of syntax?

                              cheers Chris Maunder

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Foreach may be creating state machines via yield.

                              It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups