Node.js v4 の話 #tng18

Node.js v4 の話 #tng18

東京Node学園 18時限目で発表した Node.js v4 の話です

D76231a2114896dfcc7b79ac69558b79?s=128

Yosuke Furukawa

October 08, 2015
Tweet

Transcript

  1. Node.js v4 @yosuke_furukawa

  2. @yosuke_furukawa

  3. Tokyo NodeFest 2012

  4. None
  5. I asked him …

  6. %PZPVIBWFBQMBOUPSFMFBTF W  /FYUZFBS NBZCF

  7. Tokyo NodeFest 2012

  8. 

  9. Congrats!!!!

  10. Node.js v4.x features

  11. Lots of features………….

  12. JavaScript Syntax/Features

  13. ES2015

  14. ES2015 • let/const • class • Map/Set/WeakMap/ WeakSet • Generators

    • Binary/Octal literals • Symbol • Template String literals • String.prototype.repeat • @@toStringTag • Enhanced Object Literals • unicode literals • arrow functions
  15. Your code will be changed in Node.js

  16. MyEventEmitter // v0.12 ‘use strict’; var events = require(‘events’); var

    util = require(‘util’); var EventEmitter = events.EventEmitter; var MyEventEmitter = function(data) { this.data = data; }; util.inherits(MyEventEmitter, EventEmitter); MyEventEmitter.prototype.intervalCheck = function() { var originalData = this.data; var _this = this; setInterval(function (){ if (originalData !== _this.data) { _this.emit(‘change’, new Date() + ‘: ’ + _this.data); originalData = _this.data; } }, 1000); }; module.exports = MyEventEmitter; DIFDLPXOPCKFDUQFSTFD
  17. MyEventEmitter // v4.0 ‘use strict’; const events = require(‘events’); const

    EventEmitter = events.EventEmitter; class MyEventEmitter extends EventEmitter { constructor(data) { this.data = data; } intervalCheck() { let originalData = this.data; setInterval(()=>{ if (originalData !== this.data) { this.emit(‘change’, `${new Date()}: ${this.data}`); originalData = this.data; } }, 1000); } } module.exports = MyEventEmitter;
  18. MyEventEmitter // After v4.0 ‘use strict’; const events = require(‘events’);

    const EventEmitter = events.EventEmitter; class MyEventEmitter extends EventEmitter { constructor(data) { this.data = data; } intervalCheck() { let originalData = this.data; setInterval(()=>{ if (originalData !== this.data) { this.emit(‘change’, `${new Date()}: ${this.data}`); originalData = this.data; } }, 1000); } } module.exports = MyEventEmitter; SFRVJSF͔Β͸DPOTUͰఆٛ͢Δ ෆมͳ෺ͱͯ͠มߋ͞Εͳ͍Α͏ʹ͢Δ VUJMJOIFSJUTͷ୅ΘΓʹDMBTT FYUFOETΛ࢖͏ BSSPXؔ਺Λ࢖͏ UFNQMBUFTUSJOHMJUFSBMͰจࣈྻ ݁߹Λॻ͘
  19. MyEventEmitter // v0.12 ‘use strict’; var events = require(‘events’); var

    util = require(‘util’); var EventEmitter = events.EventEmitter; var MyEventEmitter = function(data) { this.data = data; }; util.inherits(MyEventEmitter, EventEmitter); MyEventEmitter.prototype.intervalCheck = function() { var originalData = this.data; var _this = this; setInterval(function (){ if (originalData !== _this.data) { _this.emit(‘change’, new Date() + ‘: ’ + _this.data); originalData = _this.data; } }, 1000); }; module.exports = MyEventEmitter; // v4.0 ‘use strict’; const events = require(‘events’); const EventEmitter = events.EventEmitter; class MyEventEmitter extends EventEmitter { constructor(data) { this.data = data; } intervalCheck() { let originalData = this.data; setInterval(()=>{ if (originalData !== this.data) { this.emit(‘change’, `${new Date()}: ${this.data}`); originalData = this.data; } }, 1000); } } module.exports = MyEventEmitter; #FGPSF "GUFS ͖ͬ͢Γॻ͚ΔΑ͏ʹͳͬͨɻ
  20. ·ͩ • default params • rest/spread (ଟ෼v5.0Ͱenable) • tail call

    opts • Proxy • Reflect • modules ((ƅ㱼ƅ)ŋ㲎. ŞƄŰŕ!!)
  21. Strong Script

  22. Strong Script • ES6 ͷίʔυΛڧ੍ͤ͞Δ࢓૊Έ • “use strong” σΟϨΫςΟϒΛॻ͘ •

    node —strong_mode hoge.js Ͱىಈ • var Λ let/constʹม͑ͨΓɺdeleteެจΛ࢖͏ͱౖΒΕͨ Γ͢Δɻ • experimental feature
  23. “use strong” “use strong”; function foo() { // v8 4.2

    enabled (io.js v2.0 also) var obj1 = ‘aaa’; // Restrict var (use let or const instead) let args = arguments; // Restrict arguments (use Rest Params …args) if (obj1 == ‘aaa’) {} // Restrict == (use strict equal ===) if (obj1 === ‘aaa’); // Restrict empty if and for for (let n in [1,2,3]) // Restrict for-in (use for-of instead) delete obj1.key // Restrict delete operator (use Map/Set instead) // v8 4.5 enabled TODO: try let undefined = ‘aaa’; // Restrict undefined binding let obj3 = { foo : ‘aaa’}; obj3.bar = ‘123’; // Restrict undefined property access }
  24. DEMO

  25. ৄ͘͠͸

  26. ESIntl

  27. ESIntl • ECMAScript ࠃࡍԽAPI • ECMA-402 / ௨শ ESIntl •

    node͸buildͷ࣌ʹ’--with-intl=full-icu — download-all’Λࢦఆ͢Δͱ৽͘͠built-inΦ ϒδΣΫτͷIntlΦϒδΣΫτ͕༗ޮʹͳΔɻ
  28. ESIntl

  29. ESIntl 4USJOH OVN SFQMBDF  aE  aEaEaE aE H

      
  30. DEMO

  31. JavaScript Syntax/Features • ES2015ڧԽ • Strong Script͕࣮ݧతʹೖͬͯΔ • ࠃࡍԽAPI͕buildΦϓγϣϯࢦఆͰೖΕΒΕ ΔΑ͏ʹͳͬͨɻ

  32. API

  33. Buffer

  34. Buffer • Buffer#indexOfϝιου͕௥Ճ • BufferͷϦϑΝΫλϦϯά͕ਐΈɺ ArrayBufferͰॻ͖௚͞ΕΔɻ • Bufferͷ܁Γฦ͠ʹfor/ofจ͕࢖͑ΔΑ͏ʹ ͳΔɻ

  35. Bufferࠓੲ෺ޠ • ͦ΋ͦ΋Buffer ͸v0.1.90Ͱ࣮૷͞Εͨ • ͦͷ౰࣌͸ArrayBuffer͸ଘࡏ͠ͳ͔ͬͨ • ్த͔ΒArrayBuffer͕࢓༷Խ͞Ε࣮ͯ૷͞ΕͨͨΊࣅͨΑ͏ ͳػೳΛ࣋ͭAPI͕Ͱ͖ͯ͠·ͬͨɻ •

    v4.0ͷλΠϛϯάͰv8͔ΒϝϞϦΛ௚઀Ξϩέʔτ͢Δؔ਺ ͕deprecatedͱͳΓɺBufferΫϥε͸ArrayBufferΫϥεΛҾ ͖ܧ͙ܗͰ࣮૷͞Εͨɻ
  36. Buffer#indexOfϝιουͷ௥Ճ จࣈྻԽ͔ͯ͠ΒJOEFY0G͢ΔΑΓ΋ང͔ʹޮ཰͕ྑ͍ɻ

  37. Buffer instanceOf ArrayBuffer "SSBZ#V⒎FSΫϥεͷΠϯελϯεΛͦͷ··౉ͤΔ

  38. Buffer for/of var buf = new Buffer([1, 2, 3]); for

    (var b of buf) console.log(b) // 1 // 2 // 3 #V⒎FS͕JUFSBUPSʹʂʂʂ
  39. DEMO

  40. Stream

  41. Stream • Simple Stream Constructor͕Ͱ͖ͨ • StreamΛ࡞Δͷʹthrough2ͱ͔Λ࢖͏ඞཁ ͕ͳ͘ͳͬͨ

  42. Simple Stream Constructor // v0.12 var Transform = require('stream').Transform; var

    util = require('util'); util.inherits(MyTransform, Transform); function MyTransform(opts){ Transform.call(this, opts); } MyTransform.prototype._transform = function(chunk, encoding, callback){ // ͜͜Ͱ ม׵ͯ͠ ... // push͢Δ this.push(chunk); }; MyTransform.prototype._flush = function(done){ // ࠷ޙʹԿ͔͚ͨ͠Ε͹͜͜Ͱ flush ͢Δ };
  43. Simple Stream Constructor // v4.0 var transform = new stream.Transform({

    transform: function(chunk, encoding, next) { // ͜͜Ͱ ม׵ͯ͠ ... // push͢Δ this.push(chunk); }, flush: function(done) { // ࠷ޙʹԿ͔͚ͨ͠Ε͹͜͜Ͱ flush ͢Δ } }); ؆୯ʂUISPVHIͱಉ͡ॻ͖ํʂʂ
  44. REPL

  45. REPL • historyΛηʔϒͯ͘͠ΕΔΑ͏ʹͳͬͨ • ී௨͚ͩͲɺΊͬͪΌخ͍͠

  46. DEMO

  47. OS

  48. OS • os.homeDir()͕௥Ճ • ࠓ·ͰhomeσΟϨΫτϦऔΕͳ͔͚ͬͨͲ औΕΔΑ͏ʹͳͬͨ

  49. API • BufferͷϦϑΝΫλϦϯάͱindexOfͷ௥Ճ • Stream͕γϯϓϧʹ࡞ΕΔΑ͏ʹͳͬͨ • REPL͕historyΛηʔϒͯ͘͠ΕΔΑ͏ʹͳͬ ͨ • os.homeDir()͕௥Ճ

  50. Performance

  51. require͕ߴ଎Խ

  52. require͕ߴ଎Խ • ͜Ε·Ͱ͸ɺfs.statSyncͱfs.readFileSyncΛ ࢖͕ͬͯͨɺstatSync͸஗͍͠ɺ readFileSync΋ຖճϓϩύςΟݺͼग़ͯ͠͠ Δͱएׯ͔͔࣌ؒΔɻ • ಺෦తʹߴ଎ͳAPI࡞ͬͨΒ50xߴ଎Խ͞Ε ͨɻ

  53. http͕ߴ଎Խ

  54. http͕ߴ଎Խ • http-parser͕ΞοϓάϨʔυ͞Εɺߴ଎Խ͕ ೖΓɺ͞ΒʹhttpϞδϡʔϧ͕ߴ଎Խ͞Εͨ

  55. http͕ߴ଎Խ • ϝϞϦར༻཰΋վળ

  56. Performance • require͕50xߴ଎Խ • http͕8%վળ • ϝϞϦޮ཰΋Ξοϓ

  57. Deprecated

  58. ࢒೦ͳ͕Βdeprecatedʹͳͬ ͨAPIୡ

  59. domains

  60. domains͸Τϥʔ͸Ωϟον ग़དྷͯ΋͔ͦͬΒग़དྷΔ͜ͱ ͕গͳ͍ΑͶ… => deprecated

  61. fs.exist/existSync

  62. fs.existͰଘࡏ֬ೝͰ͖Δ͚ Ͳɺଘࡏ֬ೝͨ͠ޙʹผͳϓ ϩηε͔Βফ͞ΕͨΒͲ͏͢ Μͷʁ => deprecated

  63. util.isXXXX

  64. util.isXXX ࿩͢ͱ௕͍

  65. util.isObject ͕ functionΛ ObjectͱΈͳͯ͠ͳ͍ underscore/lodash͸ͪΌΜ ͱ_.isObjectͰ͸trueʹͳΔ

  66. ࣮͸JSͷ࢓༷తʹ΋Function ͸ObjectͷҰͭ

  67. मਖ਼͢Δͱmajor্͕ͬͪΌ ͏…

  68. ͦ΋ͦ΋ཁΒͳ͘Ͷʁ => deprecated

  69. Deprecated • domains • fs.exists • util.isXXXXX

  70. Conclusion

  71. Conclusion • ES.nextػೳڧԽ • API͕एׯύϫʔΞοϓ • ੑೳվળʂʂʂ • deprecatedͳAPI͸ࠓޙ࢖Θͳ͍Α͏ ʹʂʂʂʂ