Skip to content

Instantly share code, notes, and snippets.

View christianhg's full-sized avatar
🍔

Christian Grøngaard christianhg

🍔
View GitHub Profile
@sindresorhus
sindresorhus / esm-package.md
Last active April 25, 2024 04:20
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@getify
getify / 1.js
Last active March 3, 2023 09:23
is Maybe a "monad?
// is Just(..) a monad? Well, it's a monad constructor.
// Its instances are certainly monads.
function Just(v) {
return { map, chain, ap };
function map(fn) {
return Just(fn(v));
}
function chain(fn) {
return fn(v);
}
@threepointone
threepointone / for-snook.md
Last active August 26, 2023 15:43
For Snook

https://twitter.com/snookca/status/1073299331262889984?s=21

‪“‬In what way is JS any more maintainable than CSS? How does writing CSS in JS make it any more maintainable?”

‪Happy to chat about this. There’s an obvious disclaimer that there’s a cost to css-in-js solutions, but that cost is paid specifically for the benefits it brings; as such it’s useful for some usecases, and not meant as a replacement for all workflows. ‬

‪(These conversations always get heated on twitter, so please believe that I’m here to converse, not to convince. In return, I promise to listen to you too and change my opinions; I’ve had mad respect for you for years and would consider your feedback a gift. Also, some of the stuff I’m writing might seem obvious to you; I’m not trying to tell you if all people of some of the details, but it might be useful to someone else who bumps into this who doesn’t have context)‬

So the big deal about css-in-js (cij) is selectors.

A Redux Story

Developer: "Doo Doo Doo... Working on the new Redux rewrite. Gotta write some fetch actions..."

FETCH_USERS -> function fetchUsersAction() {...}
FETCH_PAGES -> function fetchPagesAction() {...}
FETCH_FRIENDS -> function fetchFriendsAction() {...}
FETCH_GROUPS -> function fetchGroupsAction() {...}
FETCH_EVENTS -> function fetchEventsAction() {...}
@getify
getify / 1.md
Last active October 15, 2020 01:44
BetterPromise: a strawman experiment in subclassing Promise and "fixing" a bunch of its awkward/bad parts

Some things that are "better" with this BetterPromise implementation:

  • BetterPromise # then(..) accepts a BetterPromise (or Promise) instance passed directly, instead of requiring a function to return it, so that the promise is linked into the chain.

    var p = BetterPromise.resolve(42);
    
    var q = Promise.resolve(10);
    
    p.then(console.log).then(q).then(console.log);
@ivenmarquardt
ivenmarquardt / lazy-evaluation.js
Last active May 18, 2018 20:37
Lazy evaluation, lazy getters, eta reduction, function composition, implicit thunks through deferred type
// Lazy Getters
/* What enables Tail Recursion Modulo Cons in Javascript is a thunk in Weak Head Normal Form.
An expression in weak head normal form has been evaluated to the outermost data constructor,
but contains sub-expressions that may not have been fully evaluated. In Javascript only thunks
can prevent sub-expressions from being immediately evaluated. */
const cons = (head, tail) => ({head, tail});
@Rich-Harris
Rich-Harris / prepack-svelte.md
Last active May 19, 2022 11:02
Is Prepack like Svelte?

Note: I'm not involved in Prepack in any way — please correct me if I say anything incorrect below!

A few people have asked me if Prepack and Svelte are similar projects with similar goals. The answer is 'no, they're not', but let's take a moment to explore why.

What is Prepack?

Prepack describes itself as a 'partial evaluator for JavaScript'. What that means is that it will run your code in a specialised interpreter that, rather than having some effect on the world (like printing a message to the console), will track the effects that would have happened and express them more directly.

So for example if you give it this code...

@developit
developit / hydra-alias.md
Last active February 27, 2020 01:54
Alias to invoke Chrome Canary w/ tracing, for IRHydra

An Alias to run Chrome with tracing enabled

For Chrome Canary:

alias hydra='/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --no-sandbox --js-flags="--user-data-dir=/tmp/profile --trace-hydrogen --trace-phase=Z --trace-deopt --code-comments --hydrogen-track-positions --redirect-code-traces"'

For regular ol' Chrome:

@staltz
staltz / adnetworks.txt
Created November 20, 2016 10:34
Ban these domains of ad networks
101com.com, 101order.com, 123found.com, 180hits.de, 180searchassistant.com, 1x1rank.com, 207.net, 247media.com, 24log.com, 24log.de, 24pm-affiliation.com, 2mdn.net, 2o7.net, 360yield.com, 4affiliate.net, 4d5.net, 50websads.com, 518ad.com, 51yes.com, 600z.com, 777partner.com, 77tracking.com, 7bpeople.com, 7search.com, 99count.com, a-ads.com, a-counter.kiev.ua, a.0day.kiev.ua, a.aproductmsg.com, a.collective-media.net, a.consumer.net, a.mktw.net, a.sakh.com, a.ucoz.net, a.ucoz.ru, a.xanga.com, a32.g.a.yimg.com, aaddzz.com, abacho.net, abc-ads.com, absoluteclickscom.com, abz.com, ac.rnm.ca, accounts.pkr.com.invalid, acsseo.com, actionsplash.com, actualdeals.com, acuityads.com, ad-balancer.at, ad-balancer.net, ad-center.com, ad-images.suntimes.com, ad-pay.de, ad-rotator.com, ad-server.gulasidorna.se, ad-serverparc.nl, ad-souk.com, ad-space.net, ad-tech.com, ad-up.com, ad.100.tbn.ru, ad.71i.de, ad.980x.com, ad.a8.net, ad.abcnews.com, ad.abctv.com, ad.about.com, ad.aboutit.de, ad.aboutwebservices.com, ad.abum.com,

Parens And Performance

Years ago, some smart folks that worked on JS engines realized that not all JS that's loaded into a page/app initially is needed right away. They implemented JIT to optimize this situation.

JIT means Just-In-Time, which means essentially that the engine can defer processing (parsing, compiling) certain parts of a JS program until a later time, for example when the function in question is actually needed. This deferral means the engine is freer to spend the important cycles right now on the code that's going to run right now. This is a really good thing for JS performance.

Some time later, some JS engine devs realized that they needed to get some hints from the code as to which functions would run right away, and which ones wouldn't. In technical speak, these hints are called heuristics.

So they realized that one very common pattern for knowing that a function was going to run right away is if the first character before the function keyword was a (, because that usually m