Skip to content

Instantly share code, notes, and snippets.

View fabiospampinato's full-sized avatar

Fabio Spampinato fabiospampinato

View GitHub Profile
@fabiospampinato
fabiospampinato / gist:7d540c073f52372d96e3d43bb39885c1
Created July 21, 2020 16:07
indexOf performance issue on v8
This file has been truncated, but you can view the full file.
const bench = ( name, fn ) => {
console.time ( name );
fn ();
console.timeEnd ( name );
};
const run = () => {
const dumb = 'a'.repeat ( 4000000 ).replace ( /[\r\0]/g, '' ); // The seemingly useless regex here makes things go faster later on
@fabiospampinato
fabiospampinato / monaco.js
Created July 24, 2020 21:15
monaco aggressive tokenization
This file has been truncated, but you can view the full file.
// The Monaco Editor can be easily created, given an
// empty container and an options literal.
// Two members of the literal are "value" and "language".
// The editor takes the full size of its container.
const WarAndPeace = `# 00 - A War and Peace
BOOK ONE: 1805
CHAPTER I
@fabiospampinato
fabiospampinato / partial.ts
Created October 15, 2020 20:16
Partial, or total, mess
type _ = {
partial: Partial
};
type Partial = {
/* WITH PLACEHOLDERS */
<T1, R> ( fn: FN<[T1], R>, a1: _ ): FN<[T1], R>,
<T1, T2, R> ( fn: FN<[T1, T2], R>, a1: _ ): FN<[T1, T2], R>,
<T1, T2, R> ( fn: FN<[T1, T2], R>, a1: _, a2: T2 ): FN<[T1], R>,
<T1, T2, R> ( fn: FN<[T1, T2], R>, a1: T1, a2: _ ): FN<[T2], R>,
@fabiospampinato
fabiospampinato / bundle-bytes.sh
Created March 18, 2021 14:49
bundle-bytes.sh
function bundle-bytes () {
#FIXME: code-only bundles, for some reaon the "--external" flags aren't respected
local full=`esbuild $@ --bundle --minify`;
local full_min=`echo $full | wc -c`;
local full_gzip=`echo $full | gzip | wc -c`;
local dependencies=`jq '.dependencies|keys[]' package.json`;
local dependencies_external=`echo $dependencies | sed -e 's/^/--external:/' | tr '\n' ' '`;
local code=`esbuild $@ --bundle --minify $dependencies_external`;
local code_min=`echo $code | wc -c`;
local code_gzip=`echo $code | gzip | wc -c`;
@fabiospampinato
fabiospampinato / build.js
Created May 23, 2021 20:21
Fast building + watching + starting + restarting. AKA how to throw TypeScript in the garbage bin for anything other than type-checking.
/* IMPORT */
const esbuild = require ( 'esbuild' );
const {nodeExternalsPlugin} = require ( 'esbuild-node-externals' );
const monex = require ( 'monex' );
const path = require ( 'path' );
const {color, parseArgv} = require ( 'specialist' );
const Watcher = require ( 'watcher' );
@fabiospampinato
fabiospampinato / charCodeAt_vs_TextEncoder.js
Created May 27, 2021 13:45
charCodeAt vs TextEncoder benchmark
const fs = require ( 'fs' );
const content = fs.readFileSync ( 'War and Peace.md', 'utf-8' );
const charCodesAt = new Array ( content.length );
console.time('charCodeAt');
for ( let i = 0, l = content.length; i < l; i++ ) {
charCodesAt[i] = content.charCodeAt ( i );
}
console.timeEnd('charCodeAt');
@fabiospampinato
fabiospampinato / fastest_escape_html.js
Created June 2, 2021 13:41
The fastest way to escape HTML strings known to me~~n~~, if you need to do so with JS and you are inside a browser.
// Can you make this faster? Ping me.
const escapeHtml = (function () {
const serializer = new XMLSerializer ();
const attr = document.createAttribute ( 'attr' );
const re = /[&<>"]/;
return function escapeHtml ( str ) {
if ( !re.test ( str ) ) return str;
attr.value = str;
return serializer.serializeToString ( attr );
@fabiospampinato
fabiospampinato / cursed_base64.ts
Created September 21, 2021 13:23
Cursed base64 encoder.
// It's the fastest pure-JS base64 encoder (that doesn't account for padding though) that I've found.
// It's cursed because it takes ~2s to startup and 16MB of memory 😂
const encoder = new TextEncoder ();
const lookup = (() => {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split ( '' );
const lookup = new Array ( 2 ** 24 );
const mask1 = 0b11111100_00000000_00000000;
const mask2 = 0b00000011_11110000_00000000;
@fabiospampinato
fabiospampinato / numbox.tsx
Last active November 4, 2021 20:44
An attempt at having Preact components with Solid-like performance (while mounted).
/* IMPORT */
import _ from 'lodash';
import {Text} from '~/components';
import {Utils} from '~/lib';
import {useRef, useClass} from '~/hooks';
import {observable, computed, Component} from '~/view';
import {Class, Styles} from './styles';
import {Props} from './types';

Password-based Authentication & E2EE

Setup

  • Encryption algorithm: aes-256-gcm
  • Hashing algorithm: sha512
  • Key derivation algorithm: pbkdf2

Signing up