• MajorHavoc@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      Why would you use anything other than Math.max?

      I mean, I might be being paid by the hour or my performance measured by lines of code…

  • auf@lemmy.ml
    link
    fedilink
    arrow-up
    87
    arrow-down
    1
    ·
    1 year ago

    Thief way is actually the best among all of these imo, in terms of readability and efficiency.

            • el_abuelo@lemmy.ml
              link
              fedilink
              arrow-up
              2
              ·
              1 year ago

              Something has gone horribly wrong if you’re trying to do such optimisations when you’ve already chosen JavaScript…let alone Electron.

                • el_abuelo@lemmy.ml
                  link
                  fedilink
                  arrow-up
                  2
                  ·
                  1 year ago

                  Thankfully the only interaction I have with teams is when a supplier arranges the call. Once every two weeks. It grosses me out every time…and that’s the Web app.

                  Do you really think they have done such optimisation efforts as minimising function calls? I can’t imagine it’s required for what is actually a fairly simple frontend app. The complexity is the enabling stack on the backend.

            • MajorHavoc@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              edit-2
              1 year ago

              Good answer.

              Even if it made me throw up in my mouth a little. /s

              Edit: Not the concept of Electron, itself - but being asked to write highly performant code in Electron.

        • Aceticon@lemmy.world
          link
          fedilink
          arrow-up
          5
          ·
          edit-2
          1 year ago

          I was under the impression that modern compilers just inline something like that, and even in older languages (like C) use trickeries are used to inline it (typically MAX is a macro rather than a real function, so its always inlined)

          Ultimatelly it depends not just on what you’re doing but also the language and compiler you’re using.

        • Demonen@lemmy.ml
          link
          fedilink
          English
          arrow-up
          3
          ·
          1 year ago

          If you’re optimizing that hard you should probably sort the data first anyway, but yeah, sometimes it’s absolutely called for. Not that I’ve actually needed that in my professional career, but then again I’ve never worked close enough to metal for it to actually matter.

          That said, all of these are implemented as functions, so they’re already costing the function call anyway…

    • snowe@programming.dev
      link
      fedilink
      arrow-up
      19
      arrow-down
      1
      ·
      1 year ago

      They’re setting a variable to a function. Just use the original function. All thief does is obfuscate for literally no gain except character count.

  • Leo Uino@lemmy.sdf.org
    link
    fedilink
    arrow-up
    74
    ·
    1 year ago

    TDD

    const max12 = (x, y) => {
        if (x === 1 && y === 2) {
            return 2;
        } else if (x === 7 && y === 4) {
            return 7;
        } else {
            return x;
        }
    };
    
  • kewko@lemdro.id
    link
    fedilink
    English
    arrow-up
    30
    ·
    1 year ago

    Mathematician 2 kinda blew my mind, kinda obvious, just can’t believe I was never taught or thought about it.

    • mac@lemm.ee
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      Lost me when it used Math.abs after calling math.max a their

    • Artyom@lemm.ee
      link
      fedilink
      arrow-up
      2
      ·
      1 year ago

      I’ve been staring at it for 10 minutes and I’m still not convinced it works.

      • uberrice@feddit.de
        link
        fedilink
        arrow-up
        3
        ·
        1 year ago

        Simple, really. Abs(x-y) is the difference between the two numbers, absolute, so positive value. So, adding abs(x-y) to the smaller of the two numbers turns it into the bigger number. Plus the bigger number, now you have 2 times the bigger number

  • Snazz@lemmy.world
    link
    fedilink
    arrow-up
    23
    ·
    1 year ago

    Bit hacker 2 is really fascinating. It uses a bit mask of all 1s (-1) or all 0s (0) and takes advantage of the fact that y ^ (x ^ y) = x and y ^ 0 = y

  • force@lemmy.world
    link
    fedilink
    English
    arrow-up
    26
    arrow-down
    5
    ·
    edit-2
    1 year ago

    wtf kind of cursed programming language is this? JS? it’s so ugly, in no universe should a function look like that

    but obviously as a rust enjoyer i have to do it like

    fn max ⟨T: PartialOrd + Copy⟩(nums: ⁊[T]) -> Option⟨T⟩ {
        let mut greatest: ⁊T = ⁊nums[0];
        match nums.len() {
            0 => None,
            1 => Some(*greatest),
            _ => {
                for num in nums {
                    if num > greatest {
                        greatest = num;
                    }
                }
                Some(*greatest)
            }
        }
    }
    

    edit: lemmy formatting REALLY hates references and generics it seems… time to go back to medieval times

    • stingpie@lemmy.world
      link
      fedilink
      arrow-up
      7
      arrow-down
      2
      ·
      1 year ago

      Ah yes, rust. The language that somehow manages to manages to as verbose as possible, with as much jargonized shorthand that a computer could handle.

      • aidan@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        1 year ago

        Exactly, I don’t understand why languages have decided that every keyword needs to be as randomly minified as possible. fn, def, rune(ok that’s not minified, just a dumb name), fmt, std. Many of these things aren’t new, but programmers recognize descriptive variable names are important, the same should be true for keywords.

    • Cihta@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      1 year ago

      Wow that’s a very exhausting language. I dropped your code into an online rust to asm converter and it actually wasn’t more! I did try to post it for fun but lemmy kept messing up the code block. Oh well, wasn’t that amusing anyway!

      • force@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        1 year ago

        lol that’s not actually how rust is written, it was just a joke

        it’d really be written

        if x > y { x } else { y }
        
        • Cihta@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          1 year ago

          Hah thanks for clarifying. I was joking too and it’s a shame I couldn’t post the results.

          Though I admit i don’t know anything about rust. I’m sure I’d like it better than the proprietary garbage i use now that just gets converted to ASM / PLC code in the end. But I can’t skip the middle man. I’m not gonna try but probably 30mins for me to “write” the above.

          Besides, how do you make money if I can code something in an hour as opposed to 2 days?

            • Cihta@lemmy.world
              link
              fedilink
              arrow-up
              3
              ·
              1 year ago

              No no no you misunderstood me.

              I was being honest, I know nothing of rust. I have however used python in embedded systems with positive results. The product didn’t make it but for other reasons.

              Funny you mention java, that’s sorta what I’m stuck in but not like you think. Beyond the fact that it’s a bloated nightmare.

              I’m just a low-level programmer at heart but I have bills to pay. The rust stuff was all just a joke… i don’t know it but maybe i should. Thanks for the info.

              Saying anymore about what I do is just super embarrassing but i promise i meant no ill will. Excuse my frustration, I’m locked into a proprietary system i have no control over. You would laugh your ass off if you saw it. Anyway, i meant no offense, have a good night!

  • Eufalconimorph@discuss.tchncs.de
    link
    fedilink
    arrow-up
    19
    ·
    1 year ago
    #define max(x,y) ( { __auto_type __x = (x); __auto_type __y = (y); __x > __y ? __x : __y; })
    

    GNU C. Also works with Clang. Avoids evaluating the arguments multiple times. The optimizer will convert the branch into a conditional move, if it doesn’t I’d replace the ternary with the “bit hacker 2” version.

  • Shhalahr@beehaw.org
    link
    fedilink
    arrow-up
    15
    ·
    1 year ago

    Procrastinator.

    Okay, but seriously: “Thief”. Why reimplement it if it’s already available in the language?