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
PRO

September 05, 2022
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

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

  2. Twitter: @yosuke_furukawa Github: yosuke-furukawa

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

  4. bun ͱ͸ • ৽͍͠ JavaScript Runtime • Node.js, Deno ͱϙδγϣϯతʹ͸ಉ͡

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

  6. ͳΜͰ bun ͍ͬͯ͏ͷʁ • bun ͸ bundle ͷ bun ͱͦΕΒΛแΈࠐΉΈ

    ͍ͨͳҙຯ͔Βத՚·ΜͷΠϝʔδ͕औΒΕ ͨΒ͍͠ɻ • YouTube Ͱݴ͚ͬͯͨͩͳͷͰɺެࣜʹ͸ॻ ͍ͯͳ͍ɻ΋͔ͨ͠͠Βͦͷ͏ͪɺ͔ͬͪΐ ͍͍ཧ༝͕͚ͭΒΕΔ͔΋ɻ
  7. bun.sh ͷ໨ࢦ͢ॴ • ߴ଎ʹىಈ͢Δ͜ͱʢEdgeͰಈ͔͢ͷ໨తʣ • ࣍ੈ୅ύϑΥʔϚϯεʢJSCʣ • πʔϧͱͯ͠ great and

    complete Λ໨ࢦ͢ ʢbundler, transpiler, package manager ͳ Ͳʣ
  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.
  9. bun.sh ͷ໨ࢦ͢ॴ • खݩͷPCͩΖ͏ͱΤοδͩΖ͏ͱશ෦JS/TSͷϥ ϯλΠϜ؀ڥΛஔ͖׵͑ΒΕΔΑ͏ʹ͍ͨ͠ • ͦͷͨΊʹɺ Web API ͱ

    Node.js API Λ྆ํ͋Δ ఔ౓ಈ͘ͱ͜Ζ·Ͱαϙʔτ͍ͯ͠Δ • Next.js ͕ಈ͘ͷ͸ͦͷ͓͔͛ɺ͜ͷลΓ͕ Deno ͱ͔ͱ͸एׯҧ͏ϚʔέςΟϯάઓུ
  10. bun.sh ͷ໨ࢦ͢ॴ • Web API • Fetch • WebSocket •

    ReadableStream • ͜ΕΒΛ࣮૷ࡁΈ
  11. bun.sh ͷ໨ࢦ͢ॴ • node_modules ͷ module resolution algorithm ΋࠷ ॳ͔Β࣮૷͍ͯ͠Δ

    • ͭ·Γɺ Node.js ͷϞδϡʔϧղܾ΋ಈ͘ • ΋ͪΖΜESM΋ಈ͘ • ESMͷ͕ୈҰࢢຽײ͕͋ΔʢESM and CommonJS are supported, but Bun internally uses ESMʣ
  12. bun.sh ͷ໨ࢦ͢ॴ • TypeScript / JSX Ͱॻ͍ͨΒͦΕΛͦͷ··τ ϥϯεύΠϧ͢Δ • APIͰ΋

    Bun.Transpiler ͷΑ͏ͳܗͰݺͼग़ͤ Δʢͭ·Γɺࣗ෼ͰͳΜ͔ϓϥάΠϯͱ͔΋ ͔͚Δʣ
  13. bun.sh ͷ໨ࢦ͢ॴ • ͳΜ͔Θ͔ΒΜ͚Ͳɺ sqlite Λ࠷ॳ͔Βಉࠝ ͍ͯ͠Δɻ import sqlite from

    "bun:sqlite"; • `.env` ϑΝΠϧͰ؀ڥม਺ϩʔυ͢Δͱ͔΋࠷ ॳ͔Β΍ͬͯ͘ΕͯΔ • ͜ͷลΓ͸DXతͳ࿩͔ɾɾɾʁ
  14. bun.sh ͷ໨ࢦ͢ॴ • Node APIʢn-apiʣ Λ࣮૷ࡁΈ • Node.js ͷnative modules

    ΋͍͍ͩͨಈ͘ Αʂͱͷ͜ͱɻ • bun:ffi ΋͋ΔɻFFIݺͼग़͠Ͱ΋ native module ͕ಈ͘
  15. ύϑΥʔϚϯε • ৭ʑ଎͍ͱ͍͏৮ΕࠐΈ • σϞͷϖʔδͰ͸଎͔ͬͨ • Α͘Α͘ݟΔͱ...

  16. ύϑΥʔϚϯε

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

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

    Λ࠾༻ͨ͠ͷ͕ຯḩͱ͍͏ ࿩ • JSCͷ͕ϝϞϦతʹޮ཰͕ྑ͍&JITͷ΍Γํ ͕ҧ͏ɻ
  19. bun ͷத਎ʹഭΔ • ߏ੒ਤ • ҰݸҰݸൺֱ͢Δ

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

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

    bun inside IUUQ RVJD૚͸ະ࣮૷
  22. node / bun

  23. node / bun 4UBOEBSEMJCSBSZ

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

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

  26. Standard Library • ઈࢍ௥ՃதͳͷͰɺͦͷ͏ͪ׬શޓ׵ʹͳΔ͔ ΋ɻ • ҰํͰຖճ௥ै͢ΔͷͭΒͦ͏ͩͳͱ΋ࢥ͏ɻ • WINTER CG

    ϕʔεͷWeb API͕ྲྀߦΕ͹ͦΕ ͚ͩαϙʔτͯ͠શ෦αϙʔτ͢Δͷ͸ແ͠ʹ ͳΔΜ͡Όͳ͍͔ɻɻɻʁͱ͍͏૝૾
  27. node / bun "#*૚ͷந৅Խ΋αϙʔτ ͞ΕͯΔڻ͍ͨ

  28. N-API • Node.js ͔Β C++, C ͳͲͷωΠςΟϒϞδϡʔ ϧΛݺͼग़࣌͢ͷந৅ԽϨΠϠʔ • bun

    Ͱطʹ࣮૷͞ΕͯΔͷͰɺ͋Δఔ౓ Node.js ͷ N-API Ͱ࡞ΒΕͨωΠςΟϒϞ δϡʔϧͳΒಈ͘ʢཧ࿦্͸ʣ • ·ͩະݕূ
  29. node / bun IUUQQBSTFS͕ҧ͏

  30. http parser vs pico http parser • Node.js ͷ http

    parser ͸ TS Ͱॻ͍ͨΒ C͕ ग़ͯ͘Δมଶύʔαʢindutny੡ʣ • pico http parser ͸CۀքͰ͸୭΋͕࢖ͬͨ͜ ͱ͋Δ kazuho ੡ߴ଎खܰύʔαʔ
  31. node / bun 7WT+4$

  32. JavaScript Τϯδϯ • JavaScript Λ࣮ߦ͢ΔΤϯδϯ • ࠷ۙͷΤϯδϯ͸େମ Just In Time

    ίϯύΠϥ͕͍ͭ ͓ͯΓɺ୯ͳΔΠϯλϓϦλͰ͸ͳ͘ɺ౷ܭతʹঢ়گ Λݟͯߴ଎Խ͞ΕΔ • ͜͜Ͱ͸ུ֓Λղઆ͠ɺΤϯδϯͷؾ࣋ͪΛ஌Δɻ • ͪͳΈʹ৘ใ͕ଟ͍ v8 ͷΤϯδϯΛࢀߟʹ͍ͯ͠Δɻ
  33. JavaScript Τϯδϯ GVODUJPOTVN B C \ SFUVSOB C ^ TVN

       <GVODUJPO LFZXPSE > <TVN JEFOUJpFS > <  QBSFO > <B JEFOUJpFS > <C JEFOUJpFS > UPLFOJ[F "45 <GVODUJPO LFZXPSE > <TVN JEFOUJpFS > <  QBSFO > <B JEFOUJpFS > <C JEFOUJpFS > 1SPHSBN 'VODUJPO%FDMBSBUJPO *EFOUJpFSTVN QBSBNT *EFOUJpFSC *EFOUJpFSB
  34. GVODUJPOTVN B C \ SFUVSOB C ^ TVN  

     ύʔαʔ ΠϯλϓϦλ +*5ίϯύΠϥ JavaScript ந৅ߏจ໦   &YDBBFDBF! $SFBUF6ONBQQFE"SHVNFOUT YDBBFDBG!G4UBSS 4YDBBFDC!G -EB/BNFE1SPQFSUZS <> <> όΠτίʔυ ػցޠ ౷ܭ৘ใ ࠷దԽ ୤࠷దԽ
  35. V8 vs JSC • Node.js ͸ v8 • bun ͸

    JSC • JIT ͢Δ࣌ʹΪΞʢม଎ػʣͷ਺͕ҧ͏
  36. JSC vs V8 • ͦ΋ͦ΋ΪΞ͸ • ௿଎: Ճ଎͸଎͍ɺ࠷ߴ଎͸஗͍ • ߴ଎:

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

    • Sparkplug: bytecode ͔Βม਺ղܾɺ୤౶ҥߏจԽ ͳͲΛ࣮ࢪ • Turbofan: ౷ܭ৘ใΛجʹܕϨϕϧͰͷ࠷దԽΛ࣮ ࢪ͢Δ
  38. JSC • Apple(Webkit) ੡ 4ஈม଎ΪΞ • LLInt: (1଎) ୯ͳΔ bytecode

    ੜ੒ • Baseline JIT: (2଎) Sparkplug ͱಉ༷ͷbytecode͔Β؆୯ͳJITੜ ੒ίʔυΛ࡞Δ • DFG JIT: (3଎) σʔλϑϩʔʹجͮ͘࠷దԽΛࢪ͢ɺओʹ໭Γ஋ͷ ܕνΣοΫͳͲΛߦ͍ɺ࠷దԽ͢Δɻ • FTL JIT: (4଎) SSA ʹΑΔ࠷దԽΛࢪ͢ɺ V8 ͩͱ Turbofan ͱಉ༷
  39. node / bun *0ந৅ԽϨΠϠ͕ҧ͏

  40. libuv vs kqueue/iouring ࣮૷ • libuv ͸ Windows ͱ͔΋αϙʔτ͢ΔͨΊʹ IOCP

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

    for Linux) ͕ ͋Γɺ ΋͏த਎ΛLinuxͱଊ͍͍͑ͯͷͰ͸આ • ͦ͏ߟ͑Δͱɺந৅Խ͕ඞཁͳͷ͸ Linux ͱ Mac ͚ͩʹ ͳΔɻ • ͡Ό͋ͬͯΜͰɺBunͰ͸ Mac ͸ kqueue ɺ Linux ͸ io_uring ͱ͍͏ग़͠෼͚͚͍ͩͯ͠Δɻ͜͜͸ݱ୅ʹ Ԋͬͨਖ਼͍͠ΞʔΩςΫνϟͳؾ΋͢Δɻ
  42. bunͷઓུ • ͜Ε·Ͱॻ͍͖ͯͯ΋෼͔Δ௨Γɺ Node.js Λஔ ͖׵͑ΔͨΊʹେ෼node.js͕Θͷ࣮૷ʹาΈدͬ ͯΔ • Deno͕͜Ε͔Β΍Ζ͏ͱ͍ͯ͠Δ Node

    friendly Λઌʹ΍͔ͬͯΒདྷͨҹ৅ • ϚʔέςΟϯάઓུతʹ໘ന͍
  43. bunͷ͜Ε͔Β • n weeks લʹࢼͨ͠ײͩ͡ͱ·ͩ·ͩɻɻɻ • Node.js ͷ API ʹޓ׵ੑ͕͋Δͱ͸ݴ͑ɺ

    100%ʹ͸ఔԕ͍ͱײͨ͡ɻ • ΋͠΋ίϯτϦϏϡʔγϣϯνϟϯεΛ࢕ͬͯ ͍Δਓ͸͔͜͜Β΍ͬͯΈΔͱ͍͍ͷ͔΋ɻ
  44. JavaScript ϥϯλΠϜͷ͜Ε͔Β • ࠓ೥͸ઓࠃ࣌୅ͷ࢝·ΓʢNode, Deno, Bun, Cloudflare Workersʣ • एׯࡾࠃ࢙ײ͕͋Δ

    • ᲇ = Node, ޖ = Deno, ᥭ = Bun తͳɻɻɻ • ࠓ·Ͱ͸ػೳࠩΛແ͘͢͜ͱʹ஫ྗ͞Ε͖ͯͨɻ • ͜Ε͔Βݸਓతʹ͸੺นͷઓ͍ʢࠩผԽʣΛݟͤͯ΄͍͠ɻ
  45. JavaScript ϥϯλΠϜͷ͜Ε͔Β • ҰํͰ WINTER CG ͱݺ͹ΕΔɺ΢Σϒޓ׵ ͷ JS ϥϯλΠϜ࢓༷ΛܾΊΑ͏ͱ͍͏ಈ͖΋

    ग़͍ͯΔɻ
  46. JavaScript ϥϯλΠϜͷ͜Ε͔Β • WINTER CG ͩͱ࠷௿ݶ͜͜·Ͱ͋Ε͹ JS ϥϯλΠϜͱͯ͠ͷඞཁ࠷௿ ݶͷ࢓༷ΛຬͨͤΔΑͶɺͱ͍͏࢓༷Λ Web

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