Skip to content

Instantly share code, notes, and snippets.

View grabcode's full-sized avatar

Alex Girard grabcode

View GitHub Profile
@grabcode
grabcode / index.html
Created July 12, 2023 13:35
Snake game
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Text-based snake game</title>
<style>
#board {
border: 3px solid;
float: left;
font-size: 24px;
line-height: 1.5ex;
@grabcode
grabcode / latency.txt
Created June 29, 2023 12:18 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@grabcode
grabcode / machine.js
Created June 13, 2020 07:42
Generated by XState Viz: https://xstate.js.org/viz
const splitA = {
id: 'splitA',
initial: 'qualifStep',
states: {
qualifStep: {
on: {
NEXT: 'emailStep'
},
},
// Geojson
// Produced by
// 1. Google My Map / Export to KMZ
// 2. Convert KMZ to GeoJSON (using https://mygeodata.cloud/converter/kmz-to-geojson)
// 3. Utilise any helpers below to produce the `output` and, through the console, run copy(JSON.stringify(output))
// Util: polygonToAllowedArea
// Convert a polygon in a Network.settings.AllowedArea ([]{latitude: number, longitude: number})
// Entry: Array[Longitude: number, Latitude: number]
@grabcode
grabcode / intervalAsync.js
Created July 26, 2017 00:40
Utility function to cope with `interval` executing asynchronous call.
/*
`setInterval` triggers a function every X secs.
It's not satifying when you deal with asynchronous functions, such as API calls.
It is indeed possible that the response comes right before the next scheduled call, or even after, defeating the purpose of intervals.
The `interval` function below aims at solving this issue.
Usage:
// real life usage, used in a redux thunk
interval(() => bodymap.get(), {
@grabcode
grabcode / seasoning.js
Created July 24, 2017 00:53
Is winter coming? `seasoning.js` adds season awareness to your programs.
/*
`seasoning.js` adds season awareness to your programs. Help finding out what season it is.
Usage:
// Get the season using the user's browser location (NOTE: it requires the user's authorisation)
seasoning.getSeason()
.then(console.log);
// Get the season using the user's geoip
seasoning.getSeason({ locationProvider: 'ipinfo' })
@grabcode
grabcode / ES6-promise-resolveAll.js
Last active July 5, 2017 03:56
resolveAll is concurrently running promises until they all either resolve or reject. Free to customise how to handle failure.
/*
2017, still not using Promises? You missed the memo.
Promise.all lets you run a serie (iteratable list) of promises but will reject as soon as one reject.
Sometimes though, you want to try resolving all of them, and then address the rejected ones. I call that "gracefully fail".
Usage (promises is an array of promise):
// Using the default error handler
resolveAll(promises)
.then(resolved => console.log('List of my resolved promises result'))
.catch(errors => {
@grabcode
grabcode / ES6-request-json.js
Last active July 5, 2017 03:44
ES6 fetch syntactic sugar: handling common fetch usage with json content type, same headers, and stringified body
/*
fetch is awesome, there's no question about it.
But aren't you tired of:
- Writing your `res => res.json()` handler
- Having to stringify your body
- Signin your request with the same headers (```{'Content-Type': json, Authorization: 'JWT ...'}```)
- Inconsistently handling the response status code and not reject promise when relevant.
Usage:
request('http://yourawesome.api.com').then(console.log).catch(console.error);
Using native nodejs packages?
https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/
How to figure out the amount of memory needed?
https://github.com/berezovskyi/lambda-test
How does my lambda consume external APIs?
https://aws.amazon.com/blogs/aws/aws-ip-ranges-json/
When a lambda fails, does it retry itself?