Skip to content

Instantly share code, notes, and snippets.

View liamgriffiths's full-sized avatar

Liam Griffiths liamgriffiths

View GitHub Profile
@liamgriffiths
liamgriffiths / traceroute.js
Last active May 10, 2024 07:40
traceroute clone in javascript
// sloppy traceroute clone
// inpired by https://blogs.oracle.com/ksplice/entry/learning_by_doing_writing_your
// and made possible by https://www.npmjs.org/package/raw-socket
var raw = require('raw-socket');
var dns = require('dns');
var target = process.argv[2] || '173.230.146.29';
var MAX_HOPS = 64;
var TIME_LIMIT = 5000;
@liamgriffiths
liamgriffiths / cors.md
Last active February 23, 2024 13:15
How CORS works

Guide to CORS

CORS (cross origin resource sharing) is a mechanism to allow client web applications make HTTP requests to other domains. For example if you load a site from http://domainA.com and want to make a request (via xhr or img src, etc) to http://domainB.com without using CORS your web browser will try to protect you by blocking the response from the other server. This is because browsers restrict responses coming from other domains via the Same-Origin-Policy.

CORS allows the browser to use reponses from other domains. This is done by including a Access-Control headers in the server responses telling the browser that requests it is making is OK and safe to use the response.

Header Description
Access-Control-Allow-Origin: Allow requests from `` to access t
@liamgriffiths
liamgriffiths / echo_server.js
Last active April 28, 2022 09:14
simple node.js net tcp/unix socket echo server
/**
* echo server
*
*/
var net = require('net'),
port = 5000,
unixsocket = '/tmp/echo.sock';
var log = function(who, what) {
@liamgriffiths
liamgriffiths / Graph.js
Last active July 28, 2019 20:39
graph. stack, and queue structures
function Graph(nodes) {
this.nodes = nodes || {};
}
Graph.prototype.print = function(storage, start, stop) {
start = start || 1;
storage.put(new Node(start));
while (! storage.isEmpty()) {
var seen = {};

Big O notation (Asymptotic Notation)

The basic idea here is to give programmers a way to compare the performance of algorithms at a very high level in a language/platform/architecture independent way. This becomes particularly important when dealing with large inputs.

Big O notion may consider the worst-case, average-case, and best-case running time of an algorithm.

When describing an algorithm using Big O notation you will drop the lower-oder values. This is because when the inputs become very large, the lower-order values become far less important. For example:

Original Becomes
@liamgriffiths
liamgriffiths / demo.jsx
Last active December 6, 2018 14:13
Sloppy switch/case demo in React.js/JSX
import React, { Component } from "react"
import ReactDOM from "react-dom"
import { Switch, Case } from "switch_case"
class App extends Component {
constructor() {
super()
this.state = {
value: false
@liamgriffiths
liamgriffiths / css_transitions.md
Last active May 15, 2017 21:39
How to use CSS3 transitions

How to CSS3 transition

on the element that you want to transition you can apply the transition css property.

transition-property refers to the css property affected by the transition - for example (top, left, margin, color, etc)

transition-duration refers to the time the animation takes place

transition-timing-funcion refers to the way the transition is applied over the duration - ease

@liamgriffiths
liamgriffiths / bits.js
Last active January 1, 2016 22:59
bit shifting hacks
var bitwiseOR = function() {
// for each bit, if there is a "1" in the place, then the bit becomes "1"
var a = parseInt('00001111', 2);
var b = parseInt('11110000', 2);
var c = a | b;
c.toString(2); // '11111111'
// when the binary number is not the same length, 0's are added to the front
var d = parseInt('11110000', 2);
var e = parseInt('1100', 2); // becomes '00001100'
@liamgriffiths
liamgriffiths / binary_heap.js
Last active January 1, 2016 22:59
binary heap and priority queue structures
/*
* Binary Heap
*
* This is a heap which is built on top of a binary tree. The binary tree can
* be built using a regular array and some arithmetic to find the array indicies
* of a particular node's children.
*
* The Binary Heap is a 'complete tree', meaning that all levels of the tree are
* completely filled before going deeper. The levels of the tree are filled from
* left to right.