Giter Club home page Giter Club logo

Comments (3)

marjakh avatar marjakh commented on June 12, 2024 4

Hi, glad that you liked the talk :)

Chrome version: 74
Flags: "--js-flags=--harmony-weak-refs --expose-gc"

Here are the demos:

<html>
  <head>
    <script>
      function log(msg) {
	  document.getElementById("log").innerHTML += msg + "<br>";
      }

      let wr;
      let stored_resolve;

      function demo() {
	  log("Starting");
      }

      function createWeakRef() {
	  let o = {hello: function() {log("Hello!");}};
	  wr = new WeakRef(o);
	  log("Created WeakRef");
	  // o goes out of scope
      }

      function peekWeakRef() {
	  if (wr.deref()) {
	      // GC can happen any time!
	      log("WeakRef is alive");
	      // KeepDuringJob -> ref kept alive.
	      wr.deref().hello();
	  } else {
	      log("WeakRef is dead");
	  }
	  // If we did this: let o = wr.deref(); that'd be a strong pointer to
	  // the object.
      }

      function doGC() {
	  log("Doing GC");
	  gc();
      }

      function peekAndGC() {
	  if (wr.deref()) {
	      log("WeakRef is alive");
	      log("Doing GC");
	      gc();
	      if (wr.deref()) {
		  log("WeakRef is still alive");
	      } else {
		  log("This should never happen!");
	      }
	  } else {
	      log("WeakRef is dead");
	  }
      }

      async function awaitCaveat() {
	  if (wr.deref()) {
	      log("WeakRef is alive, let's await...");
	      prom = new Promise(function(resolve) {
		  stored_resolve = resolve;
	      });
	      await prom;
	      // Not the same job anymore; KeepDuringJob doesn't help!
	      log("await returned");
	      if (wr.deref()) {
		  log("WeakRef is alive after await");
	      } else {
		  log("WeakRef died during await");
	      }
	  }
      }

      function doGCAndResolve() {
	  log("Doing GC");
	  gc();
	  log("Resolving promise");
	  stored_resolve();
      }
      </script>
  </head>
  <body onload="demo()">
    <div>This is Google Chrome Dev (version 74) started with "--js-flags=--harmony-weak-refs --expose-gc"</div>
    <div>Tell us what you think of weak refs! [email protected] & [email protected]</div>
    <button onClick="createWeakRef()">Create WeakRef</button>
    <button onClick="peekWeakRef()">Peek WeakRef</button>
    <button onClick="doGC()">GC</button>
    <button onClick="peekAndGC()">Peek and GC</button>
    <button onClick="awaitCaveat()">Await</button>
    <button onClick="doGCAndResolve()()">GC and resolve</button>
    <br>
    <br>
    <div id="log"></div>
  </body>
</html>

and

<html>
  <head>
    <script>
      function log(msg) {
	  document.getElementById("log").innerHTML += msg + "<br>";
      }

      let fg;
      let count = 0;
      function demo() {
	  log("Starting");
	  let cleanup = function(iter) {
	      log("Cleanup");
	      for (h of iter) {
		  // Here we get the holdings, not the object. The object is
		  // already dead! Here we cannot resurrect it.
		  log("Got holdings: " + h);
	      }
	  }
	  fg = new FinalizationGroup(cleanup);
      }

      function createObjectAndRegister() {
	  let o = {};
	  let holdings = "object number " + ++count;
	  log("Created " + holdings);
	  fg.register(o, holdings);
	  // o goes out of scope
      }
      function doGC() {
	  log("Doing GC");
	  gc();
      }
    </script>
  </head>
  <body onload="demo()">
    <div>This is Google Chrome Dev (version 74) started with "--js-flags=--harmony-weak-refs --expose-gc"</div>
    <div>Tell us what you think of weak refs! [email protected] & [email protected]</div>
    <button onClick="createObjectAndRegister()">Create object</button>
    <button onClick="doGC()">GC</button>
    <br>
    <br>
    <div id="log"></div>
  </body>
</html>

from js-kongress-munich-deep-track.

chicoxyzzy avatar chicoxyzzy commented on June 12, 2024

Hi Marja! Thank you very much for your talk at JS Kongress Deep-Track!
I want to experiment with WeakRefs, what version of V8/Chrome should I use and what flags should I turn on? Also I'd be very grateful if you can share your slides.

from js-kongress-munich-deep-track.

chicoxyzzy avatar chicoxyzzy commented on June 12, 2024

Thank you, Marja!

from js-kongress-munich-deep-track.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.