Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bun first impressions

Bun first impressions

2022/09/05 に Node 学園で発表した Bun の話です。

Yosuke Furukawa

September 05, 2022
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. Bun first impressions
    2022/09/05 @ NodeֶԂ 40࣌ݶ໨

    View full-size slide

  2. Twitter: @yosuke_furukawa
    Github: yosuke-furukawa

    View full-size slide

  3. ࠓ೔͸ bun ʹ͍ͭͯ࿩͠·͢

    View full-size slide

  4. bun ͱ͸
    • ৽͍͠ JavaScript Runtime
    • Node.js, Deno ͱϙδγϣϯతʹ͸ಉ͡
    • ಛ௃తͳॴ
    • JSC
    • Next.js ͕ಈ͘
    • TypeScript Transpiler ಉࠝ

    View full-size slide

  5. લʹࣾ಺Ͱ͸࿩͕͋ͬͨ
    • 2022/1/19 ࠒʹݹ઒ηϯαʔ͸Ωϟον͍ͯͨ͠
    • ͔ͬ͢Γ๨Εͯͨɺࠓࠒʹͳͬͯ࿩୊ʹͳͬͨͷ͸ɺ৽͘͠
    Next.jsಈ͘Α͏ʹͳͬͨͷͱ͋Δఔ౓όʔδϣϯ্͕͕ͬͯ
    ࢖͑ΔΑ͏ʹͳͬͨͷͰLPΛ৽͔ͨ͘͠͠ΒͩͱࢥΘΕΔ

    View full-size slide

  6. ͳΜͰ bun ͍ͬͯ͏ͷʁ
    • bun ͸ bundle ͷ bun ͱͦΕΒΛแΈࠐΉΈ
    ͍ͨͳҙຯ͔Βத՚·ΜͷΠϝʔδ͕औΒΕ
    ͨΒ͍͠ɻ
    • YouTube Ͱݴ͚ͬͯͨͩͳͷͰɺެࣜʹ͸ॻ
    ͍ͯͳ͍ɻ΋͔ͨ͠͠Βͦͷ͏ͪɺ͔ͬͪΐ
    ͍͍ཧ༝͕͚ͭΒΕΔ͔΋ɻ

    View full-size slide

  7. bun.sh ͷ໨ࢦ͢ॴ
    • ߴ଎ʹىಈ͢Δ͜ͱʢEdgeͰಈ͔͢ͷ໨తʣ
    • ࣍ੈ୅ύϑΥʔϚϯεʢJSCʣ
    • πʔϧͱͯ͠ great and complete Λ໨ࢦ͢
    ʢbundler, transpiler, package manager ͳ
    Ͳʣ

    View full-size slide

  8. bun.sh ͷ໨ࢦ͢ॴ
    ͜͜ʹ͘͢͝ݱΕͯΔ
    Bun is designed as a drop-in replacement for your current JavaScript
    & TypeScript apps or scripts — on your local computer, server or on
    the edge. 


    Bun natively implements hundreds of Node.js and Web APIs, including
    ~90% of Node-API functions (native modules), fs, path, Buffer and
    more.
    The goal of Bun is to run most of the worlds JavaScript outside of
    browsers, bringing performance and complexity enhancements to your
    future infrastructure, as well as developer productivity through
    better, simpler tooling.

    View full-size slide

  9. bun.sh ͷ໨ࢦ͢ॴ
    • खݩͷPCͩΖ͏ͱΤοδͩΖ͏ͱશ෦JS/TSͷϥ
    ϯλΠϜ؀ڥΛஔ͖׵͑ΒΕΔΑ͏ʹ͍ͨ͠
    • ͦͷͨΊʹɺ Web API ͱ Node.js API Λ྆ํ͋Δ
    ఔ౓ಈ͘ͱ͜Ζ·Ͱαϙʔτ͍ͯ͠Δ
    • Next.js ͕ಈ͘ͷ͸ͦͷ͓͔͛ɺ͜ͷลΓ͕
    Deno ͱ͔ͱ͸एׯҧ͏ϚʔέςΟϯάઓུ

    View full-size slide

  10. bun.sh ͷ໨ࢦ͢ॴ
    • Web API
    • Fetch
    • WebSocket
    • ReadableStream
    • ͜ΕΒΛ࣮૷ࡁΈ

    View full-size slide

  11. bun.sh ͷ໨ࢦ͢ॴ
    • node_modules ͷ module resolution algorithm ΋࠷
    ॳ͔Β࣮૷͍ͯ͠Δ
    • ͭ·Γɺ Node.js ͷϞδϡʔϧղܾ΋ಈ͘
    • ΋ͪΖΜESM΋ಈ͘
    • ESMͷ͕ୈҰࢢຽײ͕͋ΔʢESM and CommonJS are
    supported, but Bun internally uses ESMʣ

    View full-size slide

  12. bun.sh ͷ໨ࢦ͢ॴ
    • TypeScript / JSX Ͱॻ͍ͨΒͦΕΛͦͷ··τ
    ϥϯεύΠϧ͢Δ
    • APIͰ΋ Bun.Transpiler ͷΑ͏ͳܗͰݺͼग़ͤ
    Δʢͭ·Γɺࣗ෼ͰͳΜ͔ϓϥάΠϯͱ͔΋
    ͔͚Δʣ

    View full-size slide

  13. bun.sh ͷ໨ࢦ͢ॴ
    • ͳΜ͔Θ͔ΒΜ͚Ͳɺ sqlite Λ࠷ॳ͔Βಉࠝ
    ͍ͯ͠Δɻ import sqlite from "bun:sqlite";
    • `.env` ϑΝΠϧͰ؀ڥม਺ϩʔυ͢Δͱ͔΋࠷
    ॳ͔Β΍ͬͯ͘ΕͯΔ
    • ͜ͷลΓ͸DXతͳ࿩͔ɾɾɾʁ

    View full-size slide

  14. bun.sh ͷ໨ࢦ͢ॴ
    • Node APIʢn-apiʣ Λ࣮૷ࡁΈ
    • Node.js ͷnative modules ΋͍͍ͩͨಈ͘
    Αʂͱͷ͜ͱɻ
    • bun:ffi ΋͋ΔɻFFIݺͼग़͠Ͱ΋ native
    module ͕ಈ͘

    View full-size slide

  15. ύϑΥʔϚϯε
    • ৭ʑ଎͍ͱ͍͏৮ΕࠐΈ
    • σϞͷϖʔδͰ͸଎͔ͬͨ
    • Α͘Α͘ݟΔͱ...

    View full-size slide

  16. ύϑΥʔϚϯε

    View full-size slide

  17. ύϑΥʔϚϯε
    ΍ͬͯΔͷ͜Ε͚ͩɺSFOEFS5P3FBEBCMF4USFBNͷ͔ࠩʁ

    View full-size slide

  18. ͳΜͰ bun ͸଎͍ͷ͔
    • V8 Ͱ͸ͳ͘ JSC Λ࠾༻ͨ͠ͱ͍͏ͱ͜Ζͱ಺
    ෦ͷίʔυʹ zig Λ࠾༻ͨ͠ͷ͕ຯḩͱ͍͏

    • JSCͷ͕ϝϞϦతʹޮ཰͕ྑ͍&JITͷ΍Γํ
    ͕ҧ͏ɻ

    View full-size slide

  19. bun ͷத਎ʹഭΔ
    • ߏ੒ਤ
    • ҰݸҰݸൺֱ͢Δ

    View full-size slide

  20. MJCVW
    IUUQQBSTFS
    7
    0QFO44- [MJC OHIUUQ
    OBQJ
    4UBOEBSE-JCSBSJFT +BWB4DSJQU

    Node.js inside
    OHUDQ

    View full-size slide

  21. LRVFVFJPVSJOH
    QJDPIUUQQBSTFS
    +BWB4DSJQU$PSF
    #PSJOH44- [MJC
    OBQJ
    4UBOEBSE-JCSBSJFT +BWB4DSJQU
    CVOKT

    bun inside
    IUUQ RVJD૚͸ະ࣮૷

    View full-size slide

  22. node / bun
    4UBOEBSEMJCSBSZ

    View full-size slide

  23. Standard Library
    • Node.js ͱ bun ͸ҰԠޓ׵ੑ͕͋Δɻ
    • ͱ͍͏͔ࠓ࣋ͬͯઈࢍNode.jsޓ׵ͷAPIΛͨ͘͞Μ࡞ͬͯΔ

    View full-size slide

  24. Standard Library
    • ࣗ෼͕ࢼͨ͠ͱ͖͸ Buffer ͕શવಈ͔ͳ͔ͬ
    ͨɻ
    • ·͞ʹ͜Εɻ

    View full-size slide

  25. Standard Library
    • ઈࢍ௥ՃதͳͷͰɺͦͷ͏ͪ׬શޓ׵ʹͳΔ͔
    ΋ɻ
    • ҰํͰຖճ௥ै͢ΔͷͭΒͦ͏ͩͳͱ΋ࢥ͏ɻ
    • WINTER CG ϕʔεͷWeb API͕ྲྀߦΕ͹ͦΕ
    ͚ͩαϙʔτͯ͠શ෦αϙʔτ͢Δͷ͸ແ͠ʹ
    ͳΔΜ͡Όͳ͍͔ɻɻɻʁͱ͍͏૝૾

    View full-size slide

  26. node / bun
    "#*૚ͷந৅Խ΋αϙʔτ
    ͞ΕͯΔڻ͍ͨ

    View full-size slide

  27. N-API
    • Node.js ͔Β C++, C ͳͲͷωΠςΟϒϞδϡʔ
    ϧΛݺͼग़࣌͢ͷந৅ԽϨΠϠʔ
    • bun Ͱطʹ࣮૷͞ΕͯΔͷͰɺ͋Δఔ౓
    Node.js ͷ N-API Ͱ࡞ΒΕͨωΠςΟϒϞ
    δϡʔϧͳΒಈ͘ʢཧ࿦্͸ʣ
    • ·ͩະݕূ

    View full-size slide

  28. node / bun
    IUUQQBSTFS͕ҧ͏

    View full-size slide

  29. http parser vs pico http
    parser
    • Node.js ͷ http parser ͸ TS Ͱॻ͍ͨΒ C͕
    ग़ͯ͘Δมଶύʔαʢindutny੡ʣ
    • pico http parser ͸CۀքͰ͸୭΋͕࢖ͬͨ͜
    ͱ͋Δ kazuho ੡ߴ଎खܰύʔαʔ

    View full-size slide

  30. node / bun
    7WT+4$

    View full-size slide

  31. JavaScript Τϯδϯ
    • JavaScript Λ࣮ߦ͢ΔΤϯδϯ
    • ࠷ۙͷΤϯδϯ͸େମ Just In Time ίϯύΠϥ͕͍ͭ
    ͓ͯΓɺ୯ͳΔΠϯλϓϦλͰ͸ͳ͘ɺ౷ܭతʹঢ়گ
    Λݟͯߴ଎Խ͞ΕΔ
    • ͜͜Ͱ͸ུ֓Λղઆ͠ɺΤϯδϯͷؾ࣋ͪΛ஌Δɻ
    • ͪͳΈʹ৘ใ͕ଟ͍ v8 ͷΤϯδϯΛࢀߟʹ͍ͯ͠Δɻ

    View full-size slide

  32. JavaScript Τϯδϯ
    GVODUJPOTVN B C
    \
    SFUVSOBC
    ^
    TVN

    >
    >
    < QBSFO
    >
    >
    >
    UPLFOJ[F
    "45
    >
    >
    < QBSFO
    >
    >
    >
    1SPHSBN
    'VODUJPO%FDMBSBUJPO
    *EFOUJpFSTVN
    QBSBNT
    *EFOUJpFSC
    *EFOUJpFSB

    View full-size slide

  33. GVODUJPOTVN B C
    \
    SFUVSOBC
    ^
    TVN

    ύʔαʔ
    ΠϯλϓϦλ
    +*5ίϯύΠϥ
    JavaScript ந৅ߏจ໦


    &YDBBFDBF!
    $SFBUF6ONBQQFE"SHVNFOUT
    YDBBFDBG!G4UBSS
    4YDBBFDC!G
    -EB/BNFE1SPQFSUZS <> <>
    όΠτίʔυ
    ػցޠ
    ౷ܭ৘ใ
    ࠷దԽ ୤࠷దԽ

    View full-size slide

  34. V8 vs JSC
    • Node.js ͸ v8
    • bun ͸ JSC
    • JIT ͢Δ࣌ʹΪΞʢม଎ػʣͷ਺͕ҧ͏

    View full-size slide

  35. JSC vs V8
    • ͦ΋ͦ΋ΪΞ͸
    • ௿଎: Ճ଎͸଎͍ɺ࠷ߴ଎͸஗͍
    • ߴ଎: Ճ଎͸஗͍ɺ࠷ߴ଎͸଎͍
    • ͦͷ্Ͱঢ়گʹԠͯ͡ΪΞΛ੾Γସ͑Δඞཁ͕͋ΔɻҰ
    ճ࠷ߴ଎ʹͨ͠ΒͣͬͱετϨʔτͰ଎͍΋ͷ΋͋Ε
    ͹ɺϥϦʔͷΑ͏ʹ੾Γฦ͕͠ଟ͍ίʔε΋༗Δͷͱಉ
    ༷ʹɺ࠷ߴ଎Ͱͣͬͱ૸Γଓ͚ΒΕΔΘ͚Ͱ͸ͳ͍

    View full-size slide

  36. V8
    • Google ੡ 3ஈม଎ΪΞ
    • Ignition: ୯ʹ bytecode Λੜ੒͢Δ
    • Sparkplug: bytecode ͔Βม਺ղܾɺ୤౶ҥߏจԽ
    ͳͲΛ࣮ࢪ
    • Turbofan: ౷ܭ৘ใΛجʹܕϨϕϧͰͷ࠷దԽΛ࣮
    ࢪ͢Δ

    View full-size slide

  37. JSC
    • Apple(Webkit) ੡ 4ஈม଎ΪΞ
    • LLInt: (1଎) ୯ͳΔ bytecode ੜ੒
    • Baseline JIT: (2଎) Sparkplug ͱಉ༷ͷbytecode͔Β؆୯ͳJITੜ
    ੒ίʔυΛ࡞Δ
    • DFG JIT: (3଎) σʔλϑϩʔʹجͮ͘࠷దԽΛࢪ͢ɺओʹ໭Γ஋ͷ
    ܕνΣοΫͳͲΛߦ͍ɺ࠷దԽ͢Δɻ
    • FTL JIT: (4଎) SSA ʹΑΔ࠷దԽΛࢪ͢ɺ V8 ͩͱ Turbofan ͱಉ༷

    View full-size slide

  38. node / bun
    *0ந৅ԽϨΠϠ͕ҧ͏

    View full-size slide

  39. libuv vs kqueue/iouring ࣮૷
    • libuv ͸ Windows ͱ͔΋αϙʔτ͢ΔͨΊʹ IOCP / epoll /
    kqueue Λந৅Խͯ͠ɺҰͭͷAPI͔Βݺͼग़ͤΔΑ͏ʹ͍ͯ͠
    Δɻ
    • ͨͩ͠ॾʑͷҧ͍Λٵऩ͢ΔͨΊʹɺ಺෦͸ͦΜͳʹ୯७͡Όͳ
    ͍ɻ
    • ͲͪΒ͔ͱ͍͏ͱɺඇಉظIOͷϑϨʔϜϫʔΫతͳڊେͳ࣮૷
    • ҰํͰɺݱ୅Ͱ͸ͦ͜·Ͱڊେͳ࣮૷͸͍Δͷ͔...? ͱ͍͏໰୊͕
    ͋Δɻ

    View full-size slide

  40. libuv vs kqueue/iouring ࣮૷
    • Windows ʹ͸ WSL (Windows Subsystems for Linux) ͕
    ͋Γɺ ΋͏த਎ΛLinuxͱଊ͍͍͑ͯͷͰ͸આ
    • ͦ͏ߟ͑Δͱɺந৅Խ͕ඞཁͳͷ͸ Linux ͱ Mac ͚ͩʹ
    ͳΔɻ
    • ͡Ό͋ͬͯΜͰɺBunͰ͸ Mac ͸ kqueue ɺ Linux ͸
    io_uring ͱ͍͏ग़͠෼͚͚͍ͩͯ͠Δɻ͜͜͸ݱ୅ʹ
    Ԋͬͨਖ਼͍͠ΞʔΩςΫνϟͳؾ΋͢Δɻ

    View full-size slide

  41. bunͷઓུ
    • ͜Ε·Ͱॻ͍͖ͯͯ΋෼͔Δ௨Γɺ Node.js Λஔ
    ͖׵͑ΔͨΊʹେ෼node.js͕Θͷ࣮૷ʹาΈدͬ
    ͯΔ
    • Deno͕͜Ε͔Β΍Ζ͏ͱ͍ͯ͠Δ Node friendly
    Λઌʹ΍͔ͬͯΒདྷͨҹ৅
    • ϚʔέςΟϯάઓུతʹ໘ന͍

    View full-size slide

  42. bunͷ͜Ε͔Β
    • n weeks લʹࢼͨ͠ײͩ͡ͱ·ͩ·ͩɻɻɻ
    • Node.js ͷ API ʹޓ׵ੑ͕͋Δͱ͸ݴ͑ɺ
    100%ʹ͸ఔԕ͍ͱײͨ͡ɻ
    • ΋͠΋ίϯτϦϏϡʔγϣϯνϟϯεΛ࢕ͬͯ
    ͍Δਓ͸͔͜͜Β΍ͬͯΈΔͱ͍͍ͷ͔΋ɻ

    View full-size slide

  43. JavaScript ϥϯλΠϜͷ͜Ε͔Β
    • ࠓ೥͸ઓࠃ࣌୅ͷ࢝·ΓʢNode, Deno, Bun, Cloudflare
    Workersʣ
    • एׯࡾࠃ࢙ײ͕͋Δ
    • ᲇ = Node, ޖ = Deno, ᥭ = Bun తͳɻɻɻ
    • ࠓ·Ͱ͸ػೳࠩΛແ͘͢͜ͱʹ஫ྗ͞Ε͖ͯͨɻ
    • ͜Ε͔Βݸਓతʹ͸੺นͷઓ͍ʢࠩผԽʣΛݟͤͯ΄͍͠ɻ

    View full-size slide

  44. JavaScript ϥϯλΠϜͷ͜Ε͔Β
    • ҰํͰ WINTER CG ͱݺ͹ΕΔɺ΢Σϒޓ׵
    ͷ JS ϥϯλΠϜ࢓༷ΛܾΊΑ͏ͱ͍͏ಈ͖΋
    ग़͍ͯΔɻ

    View full-size slide

  45. JavaScript ϥϯλΠϜͷ͜Ε͔Β
    • WINTER CG ͩͱ࠷௿ݶ͜͜·Ͱ͋Ε͹ JS ϥϯλΠϜͱͯ͠ͷඞཁ࠷௿
    ݶͷ࢓༷ΛຬͨͤΔΑͶɺͱ͍͏࢓༷Λ Web ͷ API Λϕʔεʹ࿩͞Εͯ
    Δɻ
    • ͜͏͍͏ͷ͕Ͱ͖ͯ͘Δͱɺ΋͏গ͠ϛχϚϜͳ JS ϥϯλΠϜͷ࣮૷Ͱ
    ΋ͬͱ߈Ίౕ͕ͨग़ͯ͘Δ͔΋͠Εͳ͍ɻ
    • ଟ༷ੑΛળͱଊ͑ͯɺઓࠃ࣌୅͕ਂ͘ͳΕ͹΋ͬͱք۾͸੝Γ্͕Δ͸
    ͣɺͦΕʹ΋ظ଴ɻ
    • ͪͳΈʹ WINTER CG ͷ James ࢯ͸ jsconf.jp ʹདྷͯ͘ΕΔ͔΋͠Εͳ͍
    ͷͰɺੋඇͦͪΒ΋ΑΖ͓͘͠Ͷ͕͍͠·͢ɻ

    View full-size slide