← Back to context

Comment by insertnickname

8 years ago

Funny, I just made something like this today (but more primitive), mostly so that I won't miss Walter Bright's comments.

    // ==UserScript==
    // @name     Hacker News VIP highlighter
    // @version  1
    // @grant    none
    // @match    https://news.ycombinator.com/*
    // ==/UserScript==

    function highlightVIPs() {
      'use strict';

      const VIPs = [
        "WalterBright"
      ];

      const userTags = Array.from(document.getElementsByClassName("hnuser"));

      userTags.forEach(tag => {
        const username = tag.href.split("=")[1];
        if (VIPs.includes(username)) {
          tag.style.fontWeight = 'bold';
        }
      });
    }

    highlightVIPs();

(I'm not much of a JavaScript programmer, but it seems to work.)

Pretty cool! If you want to speed your script up, consider implementing VIPs as a Set() so lookups are approx. O(1) rather than O(n).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

  • His solution is fine -- the asymptotic complexity in this case doesn't matter since N is bounded and small.

  • Probably won't see much speedup with one VIP? b^)

    • I currently have nine usernames in the array (removed all except Walter from the comment for brevity and privacy), but it's still a pretty small `n`. I don't know at what `n` the set becomes faster than the array, but I haven't noticed any performance impact from the script, and I can't be bothered even googling how to profile it in the browser.

      3 replies →

  • Hashing sets add a large fixed cost that dominates for small n. IIRC the rule of thumb is that arrays are better up til around a dozen or two.