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
460
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.3k
JavaScript Promise API in 2019
arayaryoma
1
800
What about Temporal in JavaScript
arayaryoma
5
1.5k
var, let, const and immutable of JavaScript
arayaryoma
0
230
2018-07-31-angular-tips
arayaryoma
1
120
Other Decks in Programming
See All in Programming
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
720
N.E.X.T LEVEL
pluu
2
300
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
CSC305 Lecture 25
javiergs
PRO
0
130
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
Jakarta EE meets AI
ivargrimstad
0
230
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
300
return文におけるstd::moveについて
onihusube
1
570
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
14 Years of iOS: Lessons and Key Points
seyfoyun
1
770
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
45
7k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
How to train your dragon (web standard)
notwaldorf
88
5.7k
For a Future-Friendly Web
brad_frost
175
9.4k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
KATA
mclloyd
29
14k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Producing Creativity
orderedlist
PRO
341
39k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
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Ͱͷ࣮