Goodbye Array .reduce

Поделиться
HTML-код
  • Опубликовано: 16 дек 2024

Комментарии • 32

  • @WesBos
    @WesBos 20 часов назад +8

    03:11 - oops I forgot to resize the browser window here :|

    • @syntaxfm
      @syntaxfm  20 часов назад +1

      you silly goose

    • @twitchizle
      @twitchizle 20 часов назад

      first time seeing you mess up your vid

    • @syntaxfm
      @syntaxfm  20 часов назад +3

      wes always messes up

    • @torquebiker9959
      @torquebiker9959 19 часов назад +1

      No prob, Chrome and Firefox doesn't matter anyway. 😁

  • @anuragroy11
    @anuragroy11 12 часов назад +4

    Loved the usage of jupyter notebook for this demo! Worked really well

  • @PaulSebastianM
    @PaulSebastianM 10 часов назад +2

    For large data sets, the first function (array.reduce) is both faster and more memory-efficient in practice.

    • @doc8527
      @doc8527 9 часов назад +2

      The usage of "array.reduce" in the video is actually discouraged by v8 dev in one of the google talk years ago, when the function was just introduced, people asked that question. Even though technically you could do it, you are trying to reduce an object out of an array.
      It's really hard to reason, and completely negates its advantage and readability (that might be subjective).
      Think about why it's called "array.reduce" instead of something like "array.transform"?
      The design of "array.reduce" in general is to let you create/reduce to a single simpler value (like a string, boolean, number) out of it.
      For whatever your case, just use a regular for loop and build an object out of your iteration.
      Then you will instantly understand why "groupBy" was introduced.

    • @syntaxfm
      @syntaxfm  8 минут назад

      We tested it! ruclips.net/video/kN7TuNCv9Wk/видео.html

  • @IainSimmons
    @IainSimmons 19 часов назад +2

    It takes an iterable... So that means it would work if you passed it a Set, a Map, a HTMLCollection (or is it NodeCollection?) that is returned from document.querySelectorAll, URLQueryParams, and many others.
    Pretty sweet!

  • @pranavgoel29
    @pranavgoel29 20 часов назад +1

    Group by sounds heavenly

  • @sid06
    @sid06 18 часов назад +2

    This clarifies all the confusion I had about map vs. object and groupBy vs. reduce, and it does so in 3 minutes. Well done.

  • @codeman99-dev
    @codeman99-dev 12 часов назад

    2:40 Since the key is the object reference, you should definitely take a moment to decide if you want a Map or a WeakMap
    The difference being that the WeakMap won't prevent garbage collection of the referenced objects.

  • @alexmejias9295
    @alexmejias9295 20 часов назад +8

    What app were you using to execute the examples?

    • @WesBos
      @WesBos 20 часов назад +18

      It's a Jupyter notebook with Deno instead of Python. Testing it out for these type of explainer vids

    • @kevinwuwon3905
      @kevinwuwon3905 19 часов назад

      I'd love to know more. Specifically, have you tried other TS notebooks and why is this the best solution?

    • @SogMosee
      @SogMosee 18 часов назад +5

      yeah this looks better than quokka's console presentation

    • @CarlosBallena
      @CarlosBallena 16 часов назад +5

      Deno 2 makes this very easy to setup and get started. Just a few mins and commands and you're good to go.

    • @thomasdinh2k
      @thomasdinh2k 16 часов назад

      @@WesBos thanks for your answer, this looks like it gonna help me learning/explaining algorithm easier

  • @tombyrer1808
    @tombyrer1808 19 часов назад

    groupBy()... does that mean I don't have to use for() loops anymore?!?
    hmmm, I wonder what the performance will be?

  • @scotle9901
    @scotle9901 2 часа назад

    Is this SQL in JS?

  • @deatho0ne587
    @deatho0ne587 16 часов назад

    So nice, I have been normally writing for of loops instead of reduce since it is about the same lines. Now yes it needs a whole new array, but it my head does there first.

  • @asahfikir
    @asahfikir 12 часов назад

    Ow, this is new for me. I don't know you could use jupyter notebook with javascript. I thought it's only for python.

    • @WesBos
      @WesBos 4 часа назад

      You can now with the Deno core

  • @bart2019
    @bart2019 8 часов назад +3

    Why the hell did you use reduce in your example. It is intended to combine array items into a single result, like sum. min or max; not to convert one data structure into another. This just screams for a simple for loop.

    • @WesBos
      @WesBos 4 часа назад

      Because everyone uses reduce for this - I’m on your team, it’s too complicated. That’s the point of the video

    • @CodingGarden
      @CodingGarden 3 часа назад +1

      As a consultant I have seen many uses of reduce like this in the wild.
      Reduce allows you to take an array and turn it into a single "value" - that single value can be anything (including another array or other type of data structure).
      Reduce is especially useful if you want to speed up an array chain of .map().filter() etc. because you can do it in a single loop.
      Another fun use case is a Promise "waterfall" where each promise executes one after another - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#running_promises_in_sequence
      Readability / maintenance is another story, but because you can use reduce this way, people do.