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

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

uhyo
December 09, 2019

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

Ginza.js#7のLTです。

uhyo

December 09, 2019
Tweet

More Decks by uhyo

Other Decks in Programming

Transcript

  1. !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 ًاحسד铣׬ ($ח״׏ג䓲⿫撑ָ ⥂䭯ׅ׷ؔـآؙؑز ָ嶊ִ׋
  2. !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׾鸐ׄג⦼ח ،ؙإأ〳腉זךד
  3. !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ָ($ׁ׸ג؝٦ٕ غحָؙㄎל׸׷