ECMAScriptの視点で見る WeakRefの“正しい”使い方

993ef638264def4334358f986fb4a91d?s=47 uhyo
December 09, 2019

ECMAScriptの視点で見る WeakRefの“正しい”使い方

Ginza.js#7のLTです。

993ef638264def4334358f986fb4a91d?s=128

uhyo

December 09, 2019
Tweet

Transcript

  1. &$."4DSJQUך鋔挿ד鋅׷ 8FBL3FGךˑ姻׃ְ˒⢪ְ倯 (JO[BKT!VIZP@

  2. 荈䊹稱➜ –ؿٗٝزؒٝسؒٝآص،!-*/&吳䒭⠓爡 – +BWB4DSJQU娖䎃ֻ׵ְ –2JJUB蔓➂&$."4DSJQUةؚDPOUSJCVUJPO侧⡘ – չ+BWB4DSJQUך \^׾椚鍑ׅ׷պ – չ➙ַ׵ד׮鷄ְאֽ׷

    +BWB4DSJQUךչ垥彊ٓ؎ـٓٔպ׾㷕עպ – չ׉׹׉׹+BWB4DSJQUח䱰欽ׁ׸׉ֲז0QUJPOBM$IBJOJOH׾➙ׁ׵䗡䎿鍑铡պ זו !VIZP@
  3. !VIZP@ 5BCMFPG$POUFOUT  8FBL3FGך䗁统  &$."4DSJQUהؖك٦آ؝ؙٖءّٝךꟼ⤘  8FBL3FGכ➬圫חוך״ֲח剅ַ׸׋ךַ  8FBL3FGָ䧮ղח♷ִ׷⥂鏾כ⡦ַ

  4. !VIZP@ 8FBL3FGך䗁统 –4UBHFفٗه٦ؠٕ –8FBL3FGؔـآؙؑزפך䓲⿫撑׾⡲䧭דֹ׷װא –'JOBMJ[BUJPO(SPVQؔـآؙؑزָؖك٦آ؝ؙٖز ׁ׸׋׵؝٦ٕغحؙㄎ׿דֻ׸׷װא ˟DG8FBL3FG+BWB4DSJQUח䓲⿫撑ָװ׏גֻ׷אְדח'JOBMJ[BUJPO׮  2JJUB 㹑⠗

  5. !VIZP@ 8FBL3FGך⢽ const ref = new WeakRef(new ArrayBuffer(1024 ** 3));

    console.log(ref.deref()); // ArrayBuffer setTimeout(()=> { gc(); console.log(ref.deref()); // undefined }, 100); new WeakRef ⦼ ד⦼פך䓲⿫撑׾⡲䧭 䓲⿫撑ך⚥魦כderef ًاحسד铣׬ ($ח״׏ג䓲⿫撑ָ ⥂䭯ׅ׷ؔـآؙؑز ָ嶊ִ׋
  6. !VIZP@ 8FBL3FGך⢽嶊ִזְ㜥さ const buf = new ArrayBuffer(1024 ** 3); const

    ref = new WeakRef(buf); console.log(ref.deref()); // ArrayBuffer console.log(ref.deref() === buf); // true setTimeout(()=> { gc(); console.log(ref.deref()); // ArrayBuffer console.log(buf.byteLength); // 1073741824 }, 100); 㢌侧CVGח⦼׾Ⰵ׸׋ 䓲ֻזְ⿫撑 䓲⿫撑ך⚥魦כ 床׃׋⦼הずׄ ($䖓׮嶊ִגְזְ 㢌侧buf׾鸐ׄג⦼ח ،ؙإأ〳腉זךד
  7. !VIZP@ 'JOBMJ[BUJPO(SPVQך⢽ const group = new FinalizationGroup( iterator => console.log([...iterator]));

    let obj = new ArrayBuffer(1024 ** 3); group.register(obj, 'דְַر٦ة’); obj = null; gc(); // [‘דְַر٦ة’] ה邌爙ׁ׸׷ ؔـآؙؑزָ($ׁ׸׋ הֹחㄎעꟼ侧׾床ׅ 'JOBMJ[BUJPO(SPVQך 湊鋔㼎韋ח obj׾鷄⸇ objפך⿫撑׾嶊ׅ objָ($〳腉חז׷ objָ($ׁ׸ג؝٦ٕ غحָؙㄎל׸׷
  8. &$."4DSJQUה($

  9. !VIZP@ &$."4DSJQUה($ 2הֿ׹דծ+4ך鎉铂➬圫&$."4DSJQUדכ ($כֿ׸תדוֲ䪔׻׸גְ׋ך ˟&$."4DSJQU +BWB4DSJQUך鎉铂➬圫׾㹀׭׷㕂ꥷ鋉呓կ չ+BWB4DSJQUպהչ&$."4DSJQUպך麩ְחכ长铡֮׷ָծ娖〷涸穗箮דせ⵸ָ麩ֲ׌ֽד䊴殯כ זְהְֲךָ涪邌罏ך䭯锷կ

  10. !VIZP@ &$."4DSJQUה($ 2הֿ׹דծ+4ך鎉铂➬圫&$."4DSJQUדכ ($כֿ׸תדוֲ䪔׻׸גְ׋ך " ⡦׮㹀纏ׁ׸גְתׇ׿ד׃׋ ˟8FBL.BQך眍ח׍׳׏ה׌ֽ($פך鎉⿹ָ֮׷ָ/05&玎䏝 ˟&$."4DSJQU +BWB4DSJQUך鎉铂➬圫׾㹀׭׷㕂ꥷ鋉呓կ չ+BWB4DSJQUպהչ&$."4DSJQUպך麩ְחכ长铡֮׷ָծ娖〷涸穗箮דせ⵸ָ麩ֲ׌ֽד䊴殯כ

    זְהְֲךָ涪邌罏ך䭯锷կ
  11. !VIZP@ &$."4DSJQUה($ 2ז׈⡦׮㹀纏ׁ׸גְזַ׏׋ך " ($כفؚٗٓيך䮙⹛חⰋֻ䕦갟׾⿹רׁזְַ׵ –($כⳢ椚禸ח״׷剑黝⻉חֺׅזְ湡涸ًٌٔ⢪欽ꆀ׾⵴幾 –剑黝⻉דفؚٗٓيך䮙⹛ָ㢌׻׷ץֹדכזְ ˟($ָ䮠ת׷ְׇדفؚٗٓيךػؿؓ٦وٝأָ衅׍׋׶ׅ׷ַ׮׃׸זְָծ ػؿؓ٦وٝأכ鎉铂➬圫ד⥂鏾ׁ׸גְזְךד➙㔐כ莆㄂ז׃

  12. !VIZP@ 8FBL3FGך&$."4DSJQUפך䕦갟 2ד׮8FBL3FGכفؚٗٓيך䮙⹛ח䕦갟ׅ׷״י

  13. !VIZP@ 8FBL3FGך&$."4DSJQUפך䕦갟 2ד׮8FBL3FGכفؚٗٓيך䮙⹛ח䕦갟ׅ׷״י " ׉ֲדׅי אְח&$."4DSJQUחؖك٦آ؝ؙٖءّٝך嚊䙀ָ

  14. !VIZP@ 8FBL3FG➬圫ח鋅׷($ IUUQTUDFTQSPQPTBMXFBLSFGT ״׶䒷欽

  15. !VIZP@ 8FBL3FG➬圫ח鋅׷($ ˑֿך➬圫כؖك٦آ؝ؙٖءّٝח㼎׃ג⡦׵ך⥂鏾׮遤׻זְ˒ IUUQTUDFTQSPQPTBMXFBLSFGT ״׶䒷欽

  16. !VIZP@ ؖك٦آ؝ؙٖءّٝך➬圫♳ך㹀纏 Ⳣ椚禸כ⟣䠐ךة؎ىؚٝדˑMJWFדזְ˒ؔـآؙؑز׾8FBL3FGַ׵ 嶊׃ג׮葺ְ⥂鏾כ搀ְ宕黅ח嶊ׁ׸זְ〳腉䚍׮֮׷ • ˑMJWFדזְ˒ؔـآؙؑزؖك٦آ؝ؙٖءّٝך㼎韋ח ז׶䖤׷ؔـآؙؑزך䬄韋涸ז邌植

  17. !VIZP@ MJWFזؔـآؙؑزך㹀纏 㼛勻涸ח8FBL3FG⟃㢩ך䩛媮דفؚٗٓيַ׵錁庠  ׁ׸׷ֿה ךזְؔـآؙؑز

  18. !VIZP@ MJWFזؔـآؙؑزך㹀纏 㼛勻涸ח8FBL3FG⟃㢩ך䩛媮דفؚٗٓيַ׵錁庠  ׁ׸׷ֿה ךזְؔـآؙؑز 錁庠הכ!!!!!!!!!! ˟׮׏ה׍ׯ׿ה׃׋㹀纏ח׃״ֲהְֲ陽锷כ֮׶תׅ

  19. תה׭

  20. !VIZP@ ➬圫חֶֽ׷8FBL3FGך⥂鏾 –($ָ饯ֿ׷⥂鏾כ搀ְⳢ椚禸⣛㶷 –($㼎韋הז׷ؔـآؙؑزך⿑㺘ז㹀纏׮Ⳣ椚禸⣛㶷 –׬׃׹8FBL3FGכ⥂鏾׾幾׵ׅ չفؚٗٓيָ⥂䭯ׅ׷ؔـآؙؑزכ($ׁ׸זְպהְֲ⥂鏾׾PQUPVU〳腉

  21. !VIZP@ 8FBL3FGך䠐纏 8FBL3FGכ⥂鏾׾幾׵ׅ Ⳣ椚禸ךⵖ秈ָ幾׷ 剑黝⻉ך堣⠓ָ㟓⸇ Ⱗ⡤涸חכًٌٔך⢪欽ꆀ⵴幾

  22. !VIZP@ תה׭ 8FBL3FGכ剑黝⻉ָ׉ך㈓♧ך㶷㖈䠐纏ד֮׶ծ ➭ך湡涸ד⢪ֲךכ⡦ַָ呎劤涸חֶַ׃ְךד 罋ִ湫׃ת׃׳ֲ 'JOBMJ[BUJPO(SPVQ׮ず圫