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

どの非同期処理が良いのか勝負

 どの非同期処理が良いのか勝負

JSオジサンで発表した資料です。

Yosuke Furukawa

December 17, 2015
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. Callbackࢯ // Ұ൪୯७ɺ஍ࠈΛݟΔ͜ͱ΋͋Δɻ 'use strict'; const fs = require('fs'); fs.readFile('/path/to/foo.txt',

    (err, foo) => { if (err) { console.error(err); return; } fs.readFile('/path/to/bar.txt', (err, bar) => { if (err) { console.error(err); return; } console.log(`${foo}${bar}`); }); }); Ұ൪୯७͚ͩͲɺ஍ࠈΛݟΔ͜ͱ΋͋Δɻ
  2. Promise ࢯ 'use strict'; const fs = require('fs'); const promisify

    = require('./promisify'); const readFile = promisify(fs.readFile); Promise.all([ readFile('/path/to/foo.txt'), readFile('/path/to/bar.txt'), ]).then((results) => { console.log(results.join('')); }).catch((err) => { console.error(err); }); &4Ͱೖͬͨඪ४ඇಉظॲཧɺਓؾ͋Δ͚Ͳɺ DBODFM͕ͳ͔ͬͨΓͱएׯยखམͪײ͕͋Δɻ
  3. Stream ࢯ 'use strict'; const fs = require('fs'); const fooStream

    = fs.createReadStream('/path/to/foo.txt'); const barStream = fs.createReadStream('/path/to/bar.txt'); fooStream.pipe(process.stdout); barStream.pipe(process.stdout); Node.js ͷதͰ෯Λར͔͍ͤͯΔ࿈ଓͨ͠ σʔλΛѻ͏ͨΊॲཧɺStreamΛ੍͢Δ΋ ͷ͸NodeΛ੍͢Δ
  4. generator/yield ࢯ 'use strict'; const co = require('co'); const fs

    = require('mz/fs'); co(function* (){ const foo = yield fs.readFile('/path/to/foo.txt'); const bar = yield fs.readFile('/path/to/bar.txt'); return foo + bar; }).then((data) => { console.log(data); }); ؔ਺ͷ్தͰࢭΊΔͱ͍͏generatorؔ਺ͷಛੑΛར༻ͨ͠ ॲཧɺ ES2015Ͱ༗ޮʹɻ஗͍ͱ͍͏ᷚ΋͋Δ͕ɺɺɺ
  5. async/await ࢯ 'use strict'; const fs = require('mz/fs'); const concat

    = async function() { const foo = await fs.readFile('/path/to/foo.txt'); const bar = await fs.readFile('/path/to/bar.txt'); return foo + bar; }; console.log(concat()); ESnextͰఏҊதͷ௒৽੕ɻ࢓༷ʹೖΔͷ͸࠶དྷ೥Ҏ͔߱ʁ
  6. ͲΕ͕Ұ൪͍͍ͷΑʁ • έʔεόΠέʔε • callback => ୯७ɻͨͩ͠஍ࠈΛݟΔ͕࣌͋Δɻ • Promise =>

    ES2015 Ͱೖͬͨඪ४ඇಉظॲཧɺͳΜͱͳ͘cancel͕ͳ͍ͱ͔ยखམ ͪײ͋Δɻ • Stream => Node.js ͷதͰ෯Λར͔͍ͤͯΔ࿈ଓͨ͠σʔλΛѻ͏ͨΊॲཧɺ StreamΛ੍͢Δ΋ͷ͸NodeΛ੍͢Δ • generator / yield => ؔ਺ͷ్தͰࢭΊΔͱ͍͏generatorؔ਺ͷಛੑΛར༻ͨ͠ॲ ཧɺ ES2015Ͱ༗ޮʹɻco΍koaͱ͔͕࠾༻ͯ͠Δɻ • async await => ௒৽੕ɺ ESnext ͰఏҊத
  7. ͲΕ͕Ұ൪͍͍ͷΑʁ • έʔεόΠέʔε • callback => ୯७ɻͨͩ͠஍ࠈΛݟΔ͕࣌͋Δɻ • Promise =>

    ES2015 Ͱೖͬͨඪ४ඇಉظॲཧ • Stream => Node.js ͷதͰ෯Λར͔͍ͤͯΔ࿈ଓͨ͠σʔλΛѻ͏ͨ ΊͷIOʹಛԽͨ͠ॲཧ • generator / yield => ES2015ͰೖͬͨएׯHackyͳॲཧ • async await => ಥ೗ݱΕͨ௒৽੕ɺ ESnext ͰఏҊத ͪΐ·Ͳ͞ΜٖਓԽ͓ئ͍க͠·͢ʂʂʂʂ
  8. ͱΓ͋͑ͣɺଌͬͯΈΑ͏ • file copy ॲཧ (খ͍͞ϑΝΠϧίϐʔ 1KB x 1000) •

    callback => 0.10sec • Promise => 0.10sec • Stream => 0.09sec • generator/yield => 0.11sec
  9. ͱΓ͋͑ͣɺଌͬͯΈΑ͏ • file copy ॲཧ (খ͍͞ϑΝΠϧίϐʔ 1KB x 1000) •

    callback => 0.10sec • Promise => 0.10sec • Stream => 0.09sec • generator/yield => 0.11sec มΘΒͳ͍ʜ
  10. ͱΓ͋͑ͣɺଌͬͯΈΑ͏ • file copy ॲཧ (ൺֱతେ͖͍ϑΝΠϧίϐʔ 15MB) • callback =>

    1.51sec • Promise => 1.54sec • Stream => 1.05sec • generator/yield => 1.59sec
  11. ͱΓ͋͑ͣɺଌͬͯΈΑ͏ • file copy ॲཧ (ൺֱతେ͖͍ϑΝΠϧίϐʔ 15MB) • callback =>

    1.51sec • Promise => 1.54sec • Stream => 1.05sec • generator/yield => 1.59sec 4USFBN଎͍
  12. ͱΓ͋͑ͣɺଌͬͯΈΑ͏ • ੑೳଌఆ݁Ռ • Stream > callback ≒ Promise ≒

    generator • େ͖͍ϑΝΠϧʹͳΔ΄ͲϑΝΠϧॻ͖ग़͠ Λஞ࣍తʹॲཧͰ͖ΔͷͰStream଎͍ɻ