Skip to content

Instantly share code, notes, and snippets.

View Xavier577's full-sized avatar
:octocat:
cooking

Joseph Tsegen Xavier577

:octocat:
cooking
View GitHub Profile

Hash Map implementation in Typescript

function DJB2Hash(key: string) {
  let hash = 5381; // magic start djb2 number

  for (let i = 0; i < key.length; i++) {
    hash = (hash << 5) + hash + key.charCodeAt(i);
    // using (hash << 5 + hash) instead of hash * 33 because '<<' is operation is faster than '*' on computer cpu
    hash = hash & hash;
  }
@Xavier577
Xavier577 / websocket_nodejs_implementation.md
Last active April 22, 2023 16:29
Implementing a websocket server without any libraries with raw nodejs

Code snippet

import { createServer } from "http";
import crypto from "crypto";

const PORT = 8001;

// this is from the web-socket specification and not something that is generated
const WEBSOCKET_MAGIC_STRING_KEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";