Skip to content

Instantly share code, notes, and snippets.

function* parseNdjson(ndjson) {
let i = 0;
do {
const newline = ndjson.indexOf("\n", i);
const string = newline === -1 ? ndjson.slice(i) : ndjson.slice(i, newline);
const json = string.trim();
if (json) {
yield JSON.parse(json);
}
i = newline + 1;
// guard against circular properties built in
function flatten(array) {
const { isArray } = Array;
if (!isArray(array)) {
return [];
}
const visited = new WeakSet;
const result = [];
const stack = [array];
const map = {
"-1": "💥",
"0": "🟦",
};
const inRange = (number, max) => 0 <= number && number < max;
const neighbors = [
[-1, -1], [-1, 0], [-1, 1],
[0, -1], [ 0, 1],
const languageCache = new Map();
let visitedNodes;
let language;
const resetVisitedNodes = () => {
visitedNodes = new WeakSet();
};
const formatRegex = /\{([^\}]*)\}/gui;
@friendlyanon
friendlyanon / log.log
Last active June 23, 2019 21:19
Object iteration test
Please read my first comment before drawing your own conclusion and take the points stated there into consideration as well
--------------------------------------------------------------------------------
Object iteration test in node 12.2.0, v8 7.4.288.21-node.17
Let O be object with properties x = 1, y = 1, z = 1, then iterate over the values and sum them, storing the result in variable `total`
--------------------------------------------------------------------------------
O = { x: 1, y: 1, z: 1 }
"use strict";
const benchmark = require("benchmark");
const suite = new benchmark.Suite;
/** SETUP BEGIN */
const length = 100;
const value = "";
const arrayOf = (function () {
function* generator(length, fill) {
const limits = [
{ "x<": [90, 8] },
{ "x<": [80, 7.9] },
];
const re = /^x?([<>]=?)$|^([<>]=?)x$/i;
const comparators = {
"<": (a, b) => a < b,
"<=": (a, b) => a <= b,
">": (a, b) => a > b,
@SuppressWarnings("ForLoopReplaceableByForEach")
public final class Sum {
private Sum() {}
public static byte of(byte[] array) {
if (array == null) {
return 0;
}
byte result = 0;
for (int i = 0, length = array.length; i < length; ++i) {
const partialBinder = ((hole, rest) => {
"use strict";
function partiallyBound(fn, args, ...missing) {
const argsLen = args.length;
const missingIter = missing[Symbol.iterator]();
for (let i = 0; i < argsLen; ++i) {
switch (args[i]) {
case hole: break;
case rest: args.splice(i, 1, ...missing); return fn.apply(this, args);
default: continue;
const csvEscape = function() {
const map = {
";": ",",
"\n": "\\n"
};
const replacerRe = /./gsm;
const replacer = x => `\\x${x.charCodeAt(0).toString(16).padStart(2, 0)}`;
const escaped = Object.keys(map).map(x => x.replace(replacerRe, replacer));
const escapeRe = new RegExp(escaped.join("|"), "g");
const escape = x => map[x];