Skip to content

Instantly share code, notes, and snippets.

Gorgi Kosev spion

Block or report user

Report or block spion

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@spion
spion / a-warning.md
Last active Oct 13, 2019
C++ versus V8 versus luajit versus C benchmark - (hash) tables
View a-warning.md

Warning

This benchmark has been misleading for a while. It was originally made to demonstrate how JIT compilers can do all sorts of crazy stuff to your code - especially LuaJIT - and was meant to be a starting point of discussion about what exactly LuaJIT does and how.

As a result, its not indicative of what its performance may be on more realistic data. Differences can be expected because

  1. the text will not consist of hard-coded constants
View mobx.js
@observer class Chart(props) {
@computed get data() { return getDataWithinRange(this.props.dateRange) }
@computed get dimensions() { return getDimensions() }
@computed get xScale() { return getXScale() }
@computed get yScale() { return getYScale() }
render() {
// use this.xScale, this.yScale etc. component will re-render automatically if any of those update.
return <svg className="Chart" />
}
@spion
spion / 01-fractal-weird-design.md
Last active Sep 28, 2019
Node streams - a fractal of weird design
View 01-fractal-weird-design.md

Node streams - a fractal of weird design

and a potential refactor that could fix that

This is a list of issues and confusions I've encountered with node streams, and things I wish were designed and implemented differently. If promise-streams is ever going to change to a design that doesn't build on top of node streams, this would be a list of mistakes to avoid

  1. enc parameter - Why is encoding always passed together with the data? It should be a separate concern. It doesn't even make sense in objectMode
  2. eventemitter base - There is no reason to use an uncomposable grab-bag of stringy events as a base
  3. relying on nextTick etc for execution order - This is very unreliable and causes all sorts of unpredictable rules for implementers which are not documented anywhere.
  4. no error propagation
View gist:96b6232b151d1007a2aa396c2e2c478a

Be careful when feature flags affect any shared mutable state. This for example will cause data loss if the flag value changes during the work:

this.fancyModel = FancyModel.createInitial()
this.rustyModel = RustyModel.createInitial()

async loadData() { 
  let flag = await this.ff.isEnabled('flag');
  let data = await this.fetchData();
View understanding-js-this.md

Understanding this in JavaScript

It's easy to trip up on the meaning of this in JavaScript. The behavior is very different from other languages, which means we have to throw most preconceptions and intuition out the window.

The best way to think of this in JS is as a hidden function argument which is passed in a slightly awkward way. Instead of the normal passing of arguments:

fn(arg1, arg2, arg3)
View translate-error.js
function translateError(msg) {
var newErr = new Error(msg); // placed here to get correct stack
return e => {
newErr.originalError = e;
throw newErr;
}
}
async function asyncTask() {
const user = await UserModel.findById(1).catch(translateError('No user found'))
View typescript-generics-clarity.ts
type ExtractValuesRecusrively<T> = T extends FormState<infer U>
? { [K in keyof U]: ExtractValuesRecusrively<U[K]> }
: T extends FieldState<infer V>
? V
: never;
// vs
type ExtractValuesRecusrively<State> = State extends FormState<infer InnerForm>
? { [FieldKey in keyof InnerForm]: ExtractValuesRecusrively<InnerForm[FieldKey]> }
View 01-readable.dhall
let types = ../types.dhall sha256:e48e21b807dad217a6c3e631fcaf3e950062310bfb4a8bbcecc330eb7b2f60ed
let defaults = ../defaults.dhall sha256:4450e23dc81975d111650e06c0238862944bf699537af6cbacac9c7e471dfabe
let deployment : types.Deployment = defaults.Deployment // {
metadata = defaults.ObjectMeta // { name = "nginx" },
spec = Some ( defaults.DeploymentSpec // {
replicas = Some 2,
template = defaults.PodTemplateSpec // {
metadata = defaults.ObjectMeta // {
View exactExposedMethodReturnTypeRule.ts
import * as Lint from 'tslint';
import * as ts from 'typescript';
import { inspect } from 'util';
/**
* This rule will ensure that methods marked with the "@expose" decorator must be declared in at
* least one interface implemented by the class. It will also ensure that the return type of this
* method has no excess properties compared to those specified in the interface
*/
View async-case.ts
type PromiseState<T> = {
state: 'fulfilled', value: T
} | {state: 'pending'} | {state: 'rejected', error: Error};
let wm = new WeakMap<Promise<any>, PromiseState<any>>()
export function asyncCase<T, U>(promise: Promise<T>, handlers: {
pending?: () => U;
fulfilled?: (t: T) => U;
You can’t perform that action at this time.