1. JavaScript / Говнокод #27616

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    Steps to reproduce:
    
      var s = "a huge, huge, huge string...";
      s = s.substring(0, 5);
    
    Expected results: s takes five bytes of memory, plus some overhead.
    Actual results: s takes a huge, huge, huge amount of memory.
    Unfortunately, most String functions use substring() or no-ops internally: concatenating with empty string, trim(), slice(), match(), search(), replace() with no match, split(), substr(), substring(), toString(), trim(), valueOf().
    My workaround is:
    
    function unleakString(s) { return (' ' + s).substr(1); }
    
    But it's not satisfying, because it breaks an abstraction and forces me to think about memory allocation.

    https://bugs.chromium.org/p/v8/issues/detail?id=2869

    Status: Assigned (Open)
    Reported on: Sep 3, 2013

    3.14159265, 26 Августа 2021

    Комментарии (49)
  2. JavaScript / Говнокод #27615

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    function *foo()
    {
       yield 1;
       yield 2;
       yield 3;
    }
    
    function main()
    {
        for (const o of foo())
        {
    	console.log (o);
        }
    
    }
    
    main();

    давайте посмотрим, как TS/JS имплементирует "елды". изначально код выглядит красиво. но заглянем под капот что генерит tsc для выполнение такого кода. мне придется разбить этот ужос

    ASD_77, 24 Августа 2021

    Комментарии (671)
  3. C++ / Говнокод #27614

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    struct file_id
    {
        uint64_t        persistent;
        uint64_t        volatile_;
    
        static const size_t RAW_LENGTH  = 16;
        operator std::string() const
        { return std::string(reinterpret_cast<const char*>(&persistent), RAW_LENGTH); }
    
        // Операторы для использования file_id в качестве ключа map и 
        // unordered_map
        struct hash
        {
            std::size_t operator()(const ntdec_smb2_file_id& file_id) const
            {
                std::string s_file_id = file_id;
                std::hash<std::string> hasher;
                return hasher(s_file_id);
            }
        };
    
        bool operator == (const ntdec_smb2_file_id& other)
        {
            return std::string(*this) == std::string(other);
        }
    
        bool operator < (const ntdec_smb2_file_id& other)
        {
            return std::string(*this) < std::string(other);
        }
    
        bool operator > (const ntdec_smb2_file_id& other)
        {
            return std::string(*this) > std::string(other);
        }
    };

    operator std::string тоже UB?

    YpaHeLI_, 24 Августа 2021

    Комментарии (11)
  4. JavaScript / Говнокод #27612

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    class IntIter {
        constructor(private i = 0) {}
        next() {
    	type retType = [value: typeof this.i, done: boolean];
            if (this.i < 10) {
                return <retType>[this.i++, false];
            }
    
            return <retType>[this.i, true];
        }
    }
    
    function main() {
        let it = new IntIter();
        for (const o of it)
        {
    	print(o);
        }
    
        for (const o of "Hello")
        {
    	print(o);
        }
    }

    добавил поддержку ForOf для ES2015 и ES3; причем компилятор сам определяет какой вариант лучше юзать

    ASD_77, 23 Августа 2021

    Комментарии (4)
  5. JavaScript / Говнокод #27606

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    type a = 1;
    type i = a & a;
    type j = a | a;
    
    function main() {
        let v1: i;
        let v2: j;
        assert(sizeof(i) != sizeof(j));
        print("done.");
    }

    ну все я понял вам скучно... как насчет такого примера?

    ASD_77, 23 Августа 2021

    Комментарии (613)
  6. Куча / Говнокод #27605

    +6

    1. 1
    Раскрытие покровов. Настя, облачные технологии и Настенька.

    HACTEHbKA, 22 Августа 2021

    Комментарии (193)
  7. JavaScript / Говнокод #27604

    −1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    type int = 1;
    type int64 = 4294967296;
    
    declare function GC_get_heap_size(): int64;
    
    function main() {
        let i = 0;
    
        for (i = 0; i < 10000000; ++i) {
            const p = new int[1];
            if (i % 100000 == 0) print("Heap size = ", GC_get_heap_size());
        }
    }

    вот такая лабуда подкатили.. называется GC.. для тех кто не любит GC я добавил опцию -nogc :)

    вот так можно JIT запускать без компиляции

    tsc.exe --emit=jit --shared-libs=TypeScriptGCWrapper.dll 1.ts


    а так можно убить GC
    tsc.exe --emit=jit -nogc 1.ts

    ASD_77, 22 Августа 2021

    Комментарии (5)
  8. Куча / Говнокод #27601

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    fn main() {
        println!("Hello World!");
    }
    
    rustc --version --verbose:
    
    rustc 1.52.1 (9bc8c42bb 2021-05-09)
    binary: rustc
    commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
    commit-date: 2021-05-09
    host: powerpc-unknown-linux-gnu
    release: 1.52.1
    LLVM version: 12.0.0
    
    Error output
    
    rustc ./hello.rs
    Illegal instruction (core dumped)

    https://github.com/rust-lang/rust/issues/85238

    Open: clienthax opened this issue on May 12 · 6 comments

    3.14159265, 22 Августа 2021

    Комментарии (26)
  9. C# / Говнокод #27583

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    //Let me introduce you, to the most stupidly long and complicated solution!.
    using System.Linq;
    
    public class TwoToOne 
    {
      static string final;
      public static string Longest (string s1, string s2) 
      {
         final = "";
         if (s1.ToLower().Contains("a")){final += "a";}
         else if (s2.ToLower().Contains("a")){final += "a";}
         if (s1.ToLower().Contains("b")){final += "b";}
         else if (s2.ToLower().Contains("b")){final += "b";}
         if (s1.ToLower().Contains("c")){final += "c";}
         else if (s2.ToLower().Contains("c")){final += "c";}
         if (s1.ToLower().Contains("d")){final += "d";}
         else if (s2.ToLower().Contains("d")){final += "d";}
         if (s1.ToLower().Contains("e")){final += "e";}
         else if (s2.ToLower().Contains("e")){final += "e";}
         if (s1.ToLower().Contains("f")){final += "f";}
         else if (s2.ToLower().Contains("f")){final += "f";}
         if (s1.ToLower().Contains("g")){final += "g";}
         else if (s2.ToLower().Contains("g")){final += "g";}
         if (s1.ToLower().Contains("h")){final += "h";}
         else if (s2.ToLower().Contains("h")){final += "h";}
         if (s1.ToLower().Contains("i")){final += "i";}
         else if (s2.ToLower().Contains("i")){final += "i";}
         if (s1.ToLower().Contains("j")){final += "j";}
         else if (s2.ToLower().Contains("j")){final += "j";}
         if (s1.ToLower().Contains("k")){final += "k";}
         else if (s2.ToLower().Contains("k")){final += "k";}
         if (s1.ToLower().Contains("l")){final += "l";}
         else if (s2.ToLower().Contains("l")){final += "l";}
         if (s1.ToLower().Contains("m")){final += "m";}
         else if (s2.ToLower().Contains("m")){final += "m";}
         if (s1.ToLower().Contains("n")){final += "n";}
         else if (s2.ToLower().Contains("n")){final += "n";}
         if (s1.ToLower().Contains("o")){final += "o";}
         else if (s2.ToLower().Contains("o")){final += "o";}
         if (s1.ToLower().Contains("p")){final += "p";}
         else if (s2.ToLower().Contains("p")){final += "p";}
         if (s1.ToLower().Contains("q")){final += "q";}
         else if (s2.ToLower().Contains("q")){final += "q";}
         if (s1.ToLower().Contains("r")){final += "r";}
         else if (s2.ToLower().Contains("r")){final += "r";}
         if (s1.ToLower().Contains("s")){final += "s";}
         else if (s2.ToLower().Contains("s")){final += "s";}
         if (s1.ToLower().Contains("t")){final += "t";}
         else if (s2.ToLower().Contains("t")){final += "t";}
         if (s1.ToLower().Contains("u")){final += "u";}
         else if (s2.ToLower().Contains("u")){final += "u";}
         if (s1.ToLower().Contains("v")){final += "v";}
         else if (s2.ToLower().Contains("v")){final += "v";}
         if (s1.ToLower().Contains("w")){final += "w";}
         else if (s2.ToLower().Contains("w")){final += "w";}
         if (s1.ToLower().Contains("x")){final += "x";}
         else if (s2.ToLower().Contains("x")){final += "x";}
         if (s1.ToLower().Contains("y")){final += "y";}
         else if (s2.ToLower().Contains("y")){final += "y";}
         if (s1.ToLower().Contains("z")){final += "z";}
         else if (s2.ToLower().Contains("z")){final += "z";}
         return final;
      }
    }

    https://www.codewars.com/kata/reviews/5656b9ee8e40eb0a4a000005/groups/5895b9cc4839ff4573001512
    Если бы платили за количество, а не качество..

    groser, 21 Августа 2021

    Комментарии (8)
  10. PHP / Говнокод #27582

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    if($account['lvl']=="1"){ $exp=round($account['exp']*100/52);}
    if($account['lvl']=="2"){ $exp=round((($account['exp']-52)/(110))*100,2);}
    if($account['lvl']=="3"){ $exp=round((($account['exp']-135)/(832-135))*100,2);}
    if($account['lvl']=="4"){ $exp=round((($account['exp']-832)/(3547-832))*100,2);}
    if($account['lvl']=="5"){ $exp=round((($account['exp']-3547)/(9658-3547))*100,2);}
    if($account['lvl']=="6"){ $exp=round((($account['exp']-9658)/(15478-9658))*100,2);}
    if($account['lvl']=="7"){ $exp=round((($account['exp']-15478)/(18478-15478))*100,2);}
    if($account['lvl']=="8"){ $exp=round((($account['exp']-18478)/(30789-18478))*100,2);}
    if($account['lvl']=="9"){ $exp=round((($account['exp']-30789)/(72394-30789))*100,2);}
    if($account['lvl']=="10"){ $exp=round((($account['exp']-72394)/(138789-72394))*100,2);}
    if($account['lvl']=="11"){ $exp=round((($account['exp']-138789)/(214787-138789))*100,2);}
    if($account['lvl']=="12"){ $exp=round((($account['exp']-214787)/(398747-214787))*100,2);}
    if($account['lvl']=="13"){ $exp=round((($account['exp']-398747)/(587058-398747))*100,2);}
    if($account['lvl']=="14"){ $exp=round((($account['exp']-587058)/(824585-587058))*100,2);}
    if($account['lvl']=="15"){ $exp=round((($account['exp']-824585)/(1247858-824585))*100,2);}
    if($account['lvl']=="16"){ $exp=round((($account['exp']-1247858)/(1558789-1247858))*100,2);}
    if($account['lvl']=="17"){ $exp=round((($account['exp']-1558789)/(1985478-1558789))*100,2);}
    if($account['lvl']=="18"){ $exp=round((($account['exp']-1985478)/(2245857-1985478))*100,2);}
    if($account['lvl']=="19"){ $exp=round((($account['exp']-2245857)/(2785896-2245857))*100,2);}
    if($account['lvl']=="20"){ $exp=round((($account['exp']-2785896)/(3685478-2785896))*100,2);}
    if($account['lvl']=="21"){ $exp=round((($account['exp']-3685478)/(4169875-3685478))*100,2);}
    if($account['lvl']=="22"){ $exp=round((($account['exp']-4169875)/(5125478-4169875))*100,2);}
    if($account['lvl']=="23"){ $exp=round((($account['exp']-5125478)/(5999999-5125478))*100,2);}
    if($account['lvl']=="24"){ $exp=round((($account['exp']-5999999)/(7145877-5999999))*100,2);}
    if($account['lvl']=="25"){ $exp=round((($account['exp']-7145877)/(8791755-7145877))*100,2);}
    if($account['lvl']=="26"){ $exp=round((($account['exp']-8791755)/(10691755-8791755))*100,2);}
    if($account['lvl']=="27"){ $exp=round((($account['exp']-10691755)/(12791755-10691755))*100,2);}
    if($account['lvl']=="28"){ $exp=round((($account['exp']-12791755)/(15191755-12791755))*100,2);}
    if($account['lvl']=="29"){ $exp=round((($account['exp']-15191755)/(18091755-15191755))*100,2);}
    if($account['lvl']=="30"){ $exp=round((($account['exp']-18091755)/(21191755-18091755))*100,2);}
    if($account['lvl']=="31"){ $exp=round((($account['exp']-21191755)/(24491755-21191755))*100,2);}
    if($account['lvl']=="32"){ $exp=round((($account['exp']-24491755)/(27991755-24491755))*100,2);}
    if($account['lvl']=="33"){ $exp=round((($account['exp']-27991755)/(31691755-27991755))*100,2);}
    if($account['lvl']=="34"){ $exp=round((($account['exp']-31691755)/(35791755-31691755))*100,2);}
    if($account['lvl']=="35"){ $exp=round((($account['exp']-35791755)/(40391755-35791755))*100,2);}
    if($account['lvl']=="36"){ $exp=round((($account['exp']-40391755)/(45591755-40391755))*100,2);}
    if($account['lvl']=="37"){ $exp=round((($account['exp']-45591755)/(51491755-45591755))*100,2);}
    if($account['lvl']=="38"){ $exp=round((($account['exp']-51491755)/(58191755-51491755))*100,2);}
    if($account['lvl']=="39"){ $exp=round((($account['exp']-58191755)/(65791755-58191755))*100,2);}
    if($account['lvl']=="40"){ $exp=round((($account['exp']-65791755)/(74391755-65791755))*100,2);}
    if($account['lvl']=="41"){ $exp=round((($account['exp']-74391755)/(83991755-74391755))*100,2);}
    if($account['lvl']=="42"){ $exp=round((($account['exp']-83991755)/(94591755-83991755))*100,2);}
    if($account['lvl']=="43"){ $exp=round((($account['exp']-94591755)/(106191755-94591755))*100,2);}
    if($account['lvl']=="44"){ $exp=round((($account['exp']-106191755)/(118791755-106191755))*100,2);}
    if($account['lvl']=="45"){ $exp=round((($account['exp']-118791755)/(132391755-118791755))*100,2);}
    if($account['lvl']=="46"){ $exp=round((($account['exp']-132391755)/(146991755-132391755))*100,2);}
    if($account['lvl']=="47"){ $exp=round((($account['exp']-146991755)/(162591755-146991755))*100,2);}
    if($account['lvl']=="48"){ $exp=round((($account['exp']-162591755)/(179191755-162591755))*100,2);}
    if($account['lvl']=="49"){ $exp=round((($account['exp']-179191755)/(196791755-179191755))*100,2);}
    if($account['lvl']=="50"){ $exp=round((($account['exp']-196791755)/(215391755-196791755))*100,2);}

    Расчет % заполнения шкалы уровня в зависимости от опыта

    EndoCrinolog, 20 Августа 2021

    Комментарии (26)