Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
gotandajs-11-nodejs-recursive-readdir
Search
Araya
May 10, 2019
Programming
2
400
gotandajs-11-nodejs-recursive-readdir
Gotanda.js #11 in MobileFactory (
https://gotandajs.connpass.com/event/127779/
) での発表資料です。
Araya
May 10, 2019
Tweet
Share
More Decks by Araya
See All by Araya
Performance and cache strategy at NIKKEI
arayaryoma
0
2.1k
JavaScript Promise API in 2019
arayaryoma
1
740
What about Temporal in JavaScript
arayaryoma
5
1.4k
var, let, const and immutable of JavaScript
arayaryoma
0
210
2018-07-31-angular-tips
arayaryoma
1
110
Other Decks in Programming
See All in Programming
入門 AWS Amplify Gen2 / Introduction to AWS Amplify Gen2
genkiogasawara
1
330
코틀린으로 멀티플랫폼 만들기
pangmoo
0
150
Zero Waste, Radical Magic, and Italian Graft – Quarkus Efficiency Secrets
hollycummins
0
230
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
670
Site Reliability Engineering for GMO
pyama86
7
1k
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
VS Code をプロダクトにどう取り込むか
onomax
1
360
What We Can Learn From OSS
inouehi
0
420
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
300
使ってみよう Azure AI Document Intelligence
kosmosebi
2
300
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
Fragment Composition of GraphQL
quramy
4
730
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
237
11k
BBQ
matthewcrist
80
8.8k
Navigating Team Friction
lara
178
13k
Unsuck your backbone
ammeep
663
57k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
A Tale of Four Properties
chriscoyier
151
22k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
6
1.5k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
357
22k
Statistics for Hackers
jakevdp
789
220k
The Cost Of JavaScript in 2023
addyosmani
16
3.9k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
322
20k
Transcript
Node.jsͰͷrecursive readdirΛߟ͑Δ ͋Β @arayaryoma Gotanda.js #11 in Mobile Factory
ࣗݾհ ͋Β / Ѩ෦ ྇ຏ @arayaryoma @arayaryoma https://araya.dev גࣜձࣾϔϚλΠτ /
෭ۀϑϦʔϥϯε JavaScript/TypeScript/Go/Kotlin Angular/React/ReactNative/Next.js/Nest.js
͔͜͜Βࠇํ໘ʹ10͘Β͍า͍ͨͱ͜ΖʹΦϑΟε͕͋ΔձࣾͰಇ͍͍ͯ·͢ ޒాͷ͖ͳ͓ళϗοτεϓʔϯͰ͢
ୡ͍ͨ͜͠ͱ ͜ͷΑ͏ͳσΟϨΫτϦͰ filesΑΓԼʹ͋ΔશͯͷϑΝΠϧͷϦετΛऔಘ͍ͨ͠
·ͣߟ͑Δ͜ͱ fs.readdirʹΦϓγϣϯΛ͢ͱɺ͔͍͍ͯ͠͠ײ͡ʹऔΕΔͷͰͳ͍͔
·ͣߟ͑Δ͜ͱ વͦΜͳநతͳͷͳ͍
ੈͷதʹస͕͍ͬͯΔղܾࡦ • https://github.com/jergason/recursive-readdir • https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search • https://gist.github.com/kethinov/6658166 ͳͲ… GoogleͰʮnode.js
recursive readdirʯͱݕࡧ͢Δͱɺ ͍͔ͭ͘ղܾࡦϥΠϒϥϦ͕ݟ͔ͭΔ
ੈͷதʹస͕͍ͬͯΔղܾࡦ ͜ΕΒͷ࣮ྫͰɺ͓͓·͔ʹ 1. fs.readdirͰσΟϨΫτϦԼͷϑΝΠϧͷϦετΛऔಘ͢Δ 2. 1ͷ݁ՌΛϧʔϓΠςϨʔλͰճ͠ɺ֤ϑΝΠϧͷύεΛfs.statؔʹ͠ɺ fs.StatsΦϒδΣΫτΛಘΔ 3. fs.StatsΦϒδΣΫτʹisDirectory()ϝιου͕͋Γɺ͜ΕͰରͷϑΝΠϧ͕ σΟϨΫτϦ͔Ͳ͏͔ఆ͢Δ
4. 3ͷ݁ՌɺσΟϨΫτϦͩͬͨ߹ͦͷσΟϨΫτϦʹ͍ͭͯɺ1~3Λߦ͏ 3ͷ݁ՌɺϑΝΠϧͩͬͨ߹ྻfilesʹՃ͢Δ 5. ϧʔϓ͕ऴΘͬͨΒྻfilesΛฦ͢ ͱ͍͏ํ๏͕ͱΒΕ͍ͯΔɻ
ੈͷதʹస͕͍ͬͯΔղܾࡦ ΤϥʔϋϯυϦϯάແࢹͨ͠࠷খݶͷ࣮ྫ ಡΈ͢͞ͷͨΊʹಉظ࣮ߦ͞ΕΔؔΛ༻
ੈͷதʹస͕͍ͬͯΔղܾࡦ ݱࡏͷσΟϨΫτϦԼͷϑΝΠϧҰཡΛऔಘ
ੈͷதʹస͕͍ͬͯΔղܾࡦ fs.statSync()Ͱfs.StatsΦϒδΣΫτΛऔಘ͠ɺ σΟϨΫτϦ͔Ͳ͏͔ఆɻ ϑΝΠϧͩͬͨ߹ϑΝΠϧϦετʹՃɻ
ੈͷதʹస͕͍ͬͯΔղܾࡦ ࢠσΟϨΫτϦͷϦετΛϧʔϓͰճ͠ɺ ֤ࢠσΟϨΫτϦʹରͯؔ͠Λ࠶ؼݺͼग़͢͠Δɻ
ੈͷதʹస͕͍ͬͯΔղܾࡦ ϑΝΠϧͷϦετΛฦ͢
ੈͷதʹస͕͍ͬͯΔղܾࡦ ࣮ߦ݁Ռ
Node.js v10.10Ҏ߱Ͱfs.statΛΘͣʹॻ͚Δ fs.readdir()ʹ{withFileTypes: true}Λ͢ͱɺfilesfs.DirentΦϒδΣΫτͷྻʹͳΔ
Node.js v10.10Ҏ߱Ͱfs.statΛΘͣʹॻ͚Δ fs.DirentΦϒδΣΫτʹisDirectory() isFile()ͳͲͷϝιου͕࣮͞Ε͍ͯΔ ϑΝΠϧ໊.nameͰऔಘͰ͖Δ -> ϑΝΠϧγεςϜίʔϧΛ1σΟϨΫτϦʹ͖ͭ 1Ͱࡁ·͢͜ͱ͕Ͱ͖Δ https://github.com/nodejs/node/pull/22020
fs.Dirent ΦϒδΣΫτΛ࣮ͬͨྫ
fs.Dirent ΦϒδΣΫτΛ࣮ͬͨྫ {withFileTypes: true}Λࢦఆͯ͠ fs.DirentΦϒδΣΫτΛಘΔ dirent.isDirectoryͰ σΟϨΫτϦ͔Ͳ͏͔ఆ 1֊ʹ͖ͭ1ͷϑΝΠϧγεςϜίʔϧ
fs.Dirent ΦϒδΣΫτΛ࣮ͬͨྫ {withFileTypes: true}Λࢦఆͯ͠ fs.DirentΦϒδΣΫτΛಘΔ dirent.isDirectoryͰ σΟϨΫτϦ͔Ͳ͏͔ఆ 1֊ʹ͖ͭ1ͷϑΝΠϧγεςϜίʔϧ
ඇಉظʹ͢Δ fs.readdir()ʹίʔϧόοΫؔΛͯ݁͠ՌΛॲཧ͍͍͕ͯ͠ɺ Node.js v10 ͰɺExperimental͕ͩfs Promises API͕ఏڙ͞Ε͍ͯΔ (v12Ͱstable)
fs Promises API
fs Promises APIΛͬͯॻ͖͢
·ͱΊ •ϥΠϒϥϦΛΘͳͯ͘ɺͦ͜·Ͱଟ͘ͳ͍هड़ྔͰrecursive readdir࣮ݱͰ͖Δɻ •Node.js v10.10Ҏ͕߱͑Δ߹fs.DirentΦϒδΣΫτΛ׆༻ͯ͠ ϑΝΠϧγεςϜίʔϧͷճΛݮΒ͢͜ͱ͕Ͱ͖Δɻ •fs Promises APIΛ༻͍Δ͜ͱͰɺϑΝΠϧγεςϜૢ࡞͕PromiseϕʔεͰॻ͚Δɻ •Node.jsminor
version updateͰศརͳAPI͕Ճ͞ΕΔ͜ͱ͕͋ΔͨΊɺ LTSͷ࠷৽Λ͍ଓ͚Δબࢶ͕औΕΔͱྑ͍ɻ
ิ: direntͱ͍͏֓೦ʹ͍ͭͯ direntLinux Programmingʹ͓͍ͯɺϑΝΠϧͷใΛ࣋ͭߏମɻ readdirؔ͜ͷߏମΛฦ͢ɻ manϖʔδ( http://man7.org/linux/man-pages/man3/readdir.3.html )ΑΓɺglibcͰͷ࣮