frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Helpful function to find memory leaks in JavaScript

1•EGreg•8h ago
I had a tough time understanding where memory leaks are coming from, especially on iOS safari. I'd go into Dev Tools > Timelines tab and see the memory go up, but not sure how or where. So I wrote this function to traverse all the global objects that have been added by various software, avoiding revisiting the same objects more than once. The function is async so as not to tie up the UX too much. You can run it to start seeing where the references are being leaked.

  Q = {};

  Q.globalNames = Object.keys(window); // snapshot baseline

  Q.globalNamesAdded = function () {
   const current = Object.keys(window);
   const baseline = Q.globalNames;
   const added = [];
  
   for (let i = 0; i < current.length; i++) {
    if (!baseline.includes(current[i])) {
     added.push(current[i]);
    }
   }
   return added;
  };
  
  Q.walkGlobalsAsync = function (filterFn, options = {}) {
    const seen = new WeakSet();
    const found = new Set();
    const pathMap = new WeakMap();

    const maxDepth = options.maxDepth || 5;
    const includeStack = options.includeStack || false;
    const logEvery = options.logEvery || 100;
    const startingKeys = Q.globalNamesAdded
      ? Q.globalNamesAdded()
      : Object.keys(window);

    let totalChecked = 0;
    let matchesFound = 0;

    function walk(obj, path = 'window', depth = 0) {
      if (!obj || typeof obj !== 'object') return;
      if (seen.has(obj)) return;
      seen.add(obj);

      totalChecked++;
      if (totalChecked % logEvery === 0) {
        console.log(`Checked ${totalChecked} objects, found ${matchesFound}`);
      }

      if (filterFn(obj)) {
        found.add(obj);
        matchesFound++;
        if (includeStack) {
          pathMap.set(obj, path);
          console.log(`[FOUND] ${path}`, obj);
        } else {
          console.log(`[FOUND]`, obj);
        }
      }

      if (depth >= maxDepth) return;

      const skipKeys = obj instanceof HTMLElement
        ? new Set([
            'parentNode', 'parentElement', 'nextSibling', 'previousSibling',
            'firstChild', 'lastChild', 'children', 'childNodes',
            'ownerDocument', 'style', 'classList', 'dataset',
            'attributes', 'innerHTML', 'outerHTML',
            'nextElementSibling', 'previousElementSibling'
          ])
        : null;

      for (const key in obj) {
        if (skipKeys && skipKeys.has(key)) continue;
        try {
          walk(obj[key], path + '.' + key, depth + 1);
        } catch (e) {}
      }
    }

    let i = 0;
    function nextBatch() {
      const batchSize = 10;
      const end = Math.min(i + batchSize, startingKeys.length);

      for (; i < end; i++) {
        try {
          walk(window[startingKeys[i]], 'window.' + startingKeys[i], 0);
        } catch (e) {}
      }

      if (i < startingKeys.length) {
        setTimeout(nextBatch, 0); // Schedule next batch
      } else {
        console.log(`Done. Found ${matchesFound} retained objects.`);
        if (includeStack) {
          console.log([...found].map(obj => ({
            object: obj,
            path: pathMap.get(obj)
          })));
        } else {
          console.log([...found]);
        }
      }
    }

    nextBatch();
  };
Here is how you use it:

  Q.walkGlobalsAsync(
   obj => obj instanceof HTMLElement && !document.contains(obj),
   { includeStack: true, maxDepth: 4, logEvery: 50 }
  );
However -- note that this will NOT find objects retained by closures, even if you can find the closures themselves you're going to have to check their code manually.

Stripe Ignoring Legal Letters and Holding $800k+

1•Solsticebay•1m ago•0 comments

Go-EUVD: Go Library for Interacting with Enisa EU Vulnerability Database (EUVD)

https://github.com/kaansk/go-euvd
1•razielk•1m ago•0 comments

Tiny aquarium fish net 3D print

https://blog.qiqitori.com/2025/07/tiny-aquarium-fish-net-3d-print/
1•furkansahin•2m ago•0 comments

The Grip That Race and Identity Have on My Students

https://www.nytimes.com/2025/07/08/opinion/race-students-teach.html
1•Michelangelo11•3m ago•0 comments

Evaluating the Critical Risks of Amazon’s Nova Premier

https://www.alphaxiv.org/abs/2507.06260v1
1•papertracker•4m ago•0 comments

How I ported Penko Park to Switch

https://ghostbutter.com/blog/penko-park-switch-port-journey/
1•konsuko•5m ago•0 comments

Europe's Great Founders Must Unretire

https://www.generalist.com/p/europes-great-founders-must-unretire
1•koolherc•5m ago•0 comments

Intel CEO says it's "too late" for them to catch up with AI competition

https://www.tomshardware.com/tech-industry/intel-ceo-says-its-too-late-for-them-to-catch-up-with-ai-competition-claims-intel-has-fallen-out-of-the-top-10-semiconductor-companies-as-the-firm-lays-off-thousands-across-the-world
1•LorenDB•7m ago•0 comments

Every Company Will Need a Chief Agent Officer

https://mike-hostetler.com/blog/orchestrating-intelligence-why-every-company-will-have-a-chief-agent-officer-y4EQpjr9
2•mikehostetler•9m ago•0 comments

If I Ran X

https://werd.io/if-i-ran-x/
2•benwerd•9m ago•1 comments

Microsoft says regulations and environmental issues are cramping Euro expansion

https://www.theregister.com/2025/07/10/microsoft_says_regulations_and_environmental/
1•rntn•9m ago•0 comments

Reframe – Open‑Source ISO20022 Message Transformer in Rust

https://github.com/GoPlasmatic/Reframe
1•codetiger•10m ago•1 comments

My Freelance Journey – From Zero to Building and Running My Own Agency

https://preetsuthar.me
1•preetsuthar17•13m ago•1 comments

Andrew Ng: Building Faster with AI [video]

https://www.youtube.com/watch?v=RNJCfif1dPY
1•sandslash•14m ago•0 comments

Flix – A powerful effect-oriented programming language

https://flix.dev/
1•freilanzer•14m ago•0 comments

WPP names senior Microsoft boss Cindy Rose as new CEO

https://www.theguardian.com/business/2025/jul/10/wpp-names-senior-microsoft-boss-cindy-rose-as-new-ceo
1•mathattack•14m ago•0 comments

Epistemological Primes

1•MountainMan1312•15m ago•1 comments

Show HN: I rebuilt few years old project and now it covers my expenses

https://www.hextaui.com/
1•preetsuthar17•15m ago•0 comments

An almost catastrophic OpenZFS bug and the humans that made it

https://despairlabs.com/blog/posts/2025-07-10-an-openzfs-bug-and-the-humans-that-made-it/
1•ahlCVA•16m ago•0 comments

Heat and Team Production: Experimental Evidence from Bangladesh

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=5234644
1•PaulHoule•19m ago•0 comments

Ask HN: Handheld Digital Writing Device

1•chromiummmm•20m ago•1 comments

Show HN: Deep Learning library in Rust has released version 0.0.5

https://github.com/araxnoid-code/ROTTA-rs
1•araxnoid•20m ago•0 comments

Boxtype–Devlog (Part 1)

https://inconvergent.net/2025/boxtype-devlog/
1•surprisetalk•21m ago•0 comments

Sergey Brin calls U.N. 'transparently antisemitic' after report on tech and Gaza

https://www.washingtonpost.com/technology/2025/07/08/sergey-brin-united-nations-gaza-israel/
2•Teever•21m ago•0 comments

Inverse Triangle Inequality

https://matklad.github.io/2025/07/07/inverse-triangle-inequality.html
1•surprisetalk•21m ago•0 comments

What don't we know about LVT? Research that could advance Georgism

https://progressandpoverty.substack.com/p/what-dont-we-know-about-lvt-research
1•surprisetalk•21m ago•0 comments

Bears Will Be Boys

https://pudding.cool/2025/07/kids-books/
2•surprisetalk•22m ago•0 comments

Haskell Lenses From Scratch (2021) [video]

https://www.youtube.com/watch?v=3kduOmZ2Wxw
2•todsacerdoti•22m ago•0 comments

Nvidia becomes first company to reach $4T in market value

https://www.theguardian.com/technology/2025/jul/09/nvidia-first-company-4-trillion
1•bookofjoe•23m ago•0 comments

Block-Based Programming for Functional Programming

https://erlangforums.com/t/lightning-talk-block-based-programming-for-functional-programming-farhad-mehta-lambda-days-2025/4892
1•unripe_syntax•23m ago•0 comments