Skip to content

Instantly share code, notes, and snippets.

View julianjensen's full-sized avatar

Julian Jensen julianjensen

  • Montgomery Village, MD
View GitHub Profile
@julianjensen
julianjensen / load-image.js
Created December 14, 2021 14:38
Load and display an image from an arbitrary URL
/**
* @param {string} url - image url
* @param {HTMLImageElement} [img] - optional pre-existing image element
* @param {HTMLElement} [parent] - optional parent element for creation of new image
* @return {Promise<any>}
*/
const addImage = ( url, img, parent ) => new Promise( resolve => {
if ( parent != null ) parent.appendChild( img = document.createElement( 'img' ) );
img.onload = () => { img.onload = null; resolve(); }
img.src = url;
@julianjensen
julianjensen / mixed-functional-component.js
Created September 25, 2020 13:29
Comparison of traditional React component with `redux` and functional component with `redux` and hooks
/**
* This is a functional component that uses hooks and `redux` and duplicates the functionality
* of our more traditional component. Without the `redux` `connect` function, we'd have to
* wrap every component in a `useMemo()` to achieve the same result which I haven't done here.
*/
const StandardComponent = ({ userName, greeting = 'Hello', answeredItAlready, setOkay }) => {
// Added to show that hooks work with `redux` state. The hook must be in all
// execution paths and must be in the `render` function. If we want to move it elsewhere
// then we'd have to make a custom hook that contained other hooks, and call the custom hook
// from here. Very messy.
@julianjensen
julianjensen / misc.css
Created July 14, 2020 01:27
CSS snippets worth remembering
/*
* Scrolls element like absolute until some scollOffset is reached then becomes sticky
*/
#toc {
position: fixed;
top: 11em;
}
@supports (top: max(1em, 1px)) {
#toc {
@julianjensen
julianjensen / simple-server.js
Created June 7, 2020 16:33
A very basic HTTP server that supports standard middleware and express-style URLs.
/** ******************************************************************************************************************
* @file A very basic HTTP server that supports standard middleware and express-style URLs.
* @date 07-Jun-2020
*********************************************************************************************************************/
"use strict";
async function MinimalServer( { PROTOCOL = 'http', host = '127.0.0.1', port = 3000 } )
{
const protocol = await import( PROTOCOL );
const protoMethods = protocol.METHODS.map( m => m.toLowerCase() );
@julianjensen
julianjensen / stack-example.md
Last active January 23, 2021 22:46
Function mechanics and stacks in gory technical detail

Some quick assembly language notes required for the following. You'll need to understand three registers on the Intel CPU.

First, the rip register. On a 16-bit CPU, it's called IP (for Instruction Pointer), and on a 32-bit CPU, it's called eip (for extended instruction pointer), but on a 64-bit modern processor, it's called rip. It holds the index (or offset or number) of the memory location where the CPU is currently executing instructions from. As it reads instructions from memory, it increments to get to the next instruction and so on. Some instructions can cause it to jump to a non-sequential read, such as a branch, jump, call, or return, for example. You can also write to it directly but this is rarely done in practice.

Second, the rsp register (also sp on 16-bit and esp on 32-bit) is the stack pointer. It points to the top of the stack. The stack grows downward. In other words, if the rsp points to memory address 10000 and you push a 32-bit number (4 bytes) onto the stack, then afterward

@julianjensen
julianjensen / permutations.md
Last active March 10, 2020 01:13
Array variations

Permutations

This problem is not that hard if you approach it the easy way, think about it "correctly." If not, then it takes a surprisingly long time to get right. Or maybe I'm just dense. The solution is quite straightforward and after I wrote it, I was surprised that it had taken me the better part of an hour to get completely right and polished. You may be able to crank it out in a few minutes.

You're given an array of whatever values. Some, all, or none of these values can, in turn, be another array, and so on.

@julianjensen
julianjensen / gist:80718f7a9ae4c3ed7c68969a52c49f43
Created February 27, 2020 03:47
Base64 encoder comments and solutions
// You don't need to a `split("")` here, you can use a string with an index, i.e. `lookupTable[n]`
// Also, while I recognize that the spread operator is slow, I'd be remiss if I didn't mention `[..."ABCD...89+/]`
const lookupTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(
"" // Why is this on a line by itself? It's confusing. A more readable `.split( "" )` is better
);
// Or `[...lookupTable].reverse()` a bit more succinct and terse, shows your intent by using `.reverse()`
const reverseLookupTable = lookupTable.reduce((acc, char, idx) => {
acc[char] = idx;
return acc;
@julianjensen
julianjensen / vowel-count.js
Created May 6, 2018 16:08
Various ways of counting vowels with benchmarks
/** ******************************************************************************************************************
* @file This just counts the number of vowels in some text.
*
* To install and play with this, do the following:
*
* 1. `mkdir vowel-count`
* 2. `cd vowel-count`
* 3. `npm init`
* 4. Copy this file into the directory, call it anything you like, but `index.js` is normal.
* 5. `npm i benchmark cli-table2`
@julianjensen
julianjensen / JavaScript Native to User-land Speed Comparisons
Created March 31, 2018 23:10
Benchmarks (taken from fast.js) comparing built-in (native) implementation versus user-land implementations.
Node.js version 7.9, Apr. 2017 Node.js version 9.9, Mar. 2018
Native .fill() vs fast.fill() (3 items) Native .fill() vs fast.fill() (3 items)
✓ Array.prototype.fill() x 26,557,003 ops/sec ±1.88% (90 runs sampled) ✓ Array.prototype.fill() x 31,067,272 ops/sec ±1.43% (90 runs sampled)
✓ Vector.fill() x 83,198,767 ops/sec ±4.40% (84 runs sampled) ✓ Vector.fill() x 147,385,123 ops/sec ±1.34% (89 runs sampled)
✓ fast.fill() x 83,882,082 ops/sec ±5.03% (84 runs sampled) ✓ fast.fill() x 140,299,152 ops/sec ±1.53% (88 runs sampled)
Result: fast.js is 0.00% faster than Vector.fill(). Result: fast.js is 0.00% faster than Array.prototype.fill().
Node.js version 7.9, Apr. 2017 Node.js version 9.9, Mar. 2018
Native .fill() vs fast.fill() (10 items)
@julianjensen
julianjensen / fibonacci.js
Created December 26, 2017 01:10
Very tiny fibonacci
const
// Direct
sqr5 = Math.sqrt( 5 ),
sqr5l = ( 1 + sqr5 ) / 2,
sqr5r = ( 1 - sqr5 ) / 2,
dfib = n => n < 2 ? n : ( ( Math.pow( sqr5l, n ) - Math.pow( sqr5r, n ) ) / sqr5 ),
// ES6
fib = ( ( pp = 0, p = 0 ) => () => p = pp + ( ( pp = p ) || 1 ) )(),
// ES5.1
ffib = ( function( pp, p ) { pp = p = 0; return function() { return p = pp + ( ( pp = p ) || 1 ); }; } )();