Comments (3)
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.
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.
Thank you, Marja!
from js-kongress-munich-deep-track.
Related Issues (20)
- JavaScript & SEO - opening up a black box
- [v8 team proposal] AMA on V8 with @verwaest and @bmeurer
- TC39 -- What's coming with classes?
- TC39 -- Immutable values, operator overloading, and custom numeric literals HOT 2
- TC39 -- AMA
- Plugins and extension systems
- [v8 team proposal] Surfacing JavaScript engine signals (i.e. optimization, deoptimization, etc.)
- Are You Sure You Are Not a Micromanager?
- How to cook with RxJS
- Technical SEO Q&A HOT 3
- Chrome DevTools Office Hours
- What's happening with Puppeteer?
- JavaScript debugging with Chrome DevTools
- A blueprint for an ideal CI/CD process in javascript projects
- Create a PWA template for instant and offline pages
- Web Components - harmony and connection in the JS world
- RxJS 8 and beyond
- Contribute a Feature to TypeScript! An example based on the ES Private Fields implementation
- Immutable data structures in JS? A deep dive in the Record & Tuple Stage 1 Proposal HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from js-kongress-munich-deep-track.