This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The non-performance-intensive parts of this implementation can remain JavaScript. | |
import fs from 'node:fs/promises' | |
const BYTES_PER_INT32 = 4 | |
const BYTES_PER_FLOAT64 = 8 | |
const BYTES_PER_PAGE = 65_536 | |
const wasmBuffer = await fs.readFile('./held-karp.wasm') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SIGN_BITS = 1n | |
const EXPONENT_BITS = 11n | |
const MANTISSA_BITS = 52n | |
const BIAS = 1023n | |
export const stringify = value => { | |
if (typeof value !== 'number') { | |
throw Error('Not a number') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item { | |
constructor (name, sellIn, quality) { | |
this.name = name | |
this.sellIn = sellIn | |
this.quality = quality | |
} | |
} | |
class Shop { | |
constructor (items = []) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exports a function which determines whether a JavaScript value is a power of | |
// two or not. Unlike the npm package `is-power-of-two`, this actually returns | |
// the correct answer in all cases. | |
const countSetBits = i => { | |
// Based on <https://stackoverflow.com/a/109025/792705> but with some of the | |
// performance optimisations reverted for the sake of improved legibility. | |
i = (i & 0b01010101010101010101010101010101) + ((i & 0b10101010101010101010101010101010) >>> 1) | |
i = (i & 0b00110011001100110011001100110011) + ((i & 0b11001100110011001100110011001100) >>> 2) | |
i = (i & 0b00001111000011110000111100001111) + ((i & 0b11110000111100001111000011110000) >>> 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { UNICODE, seq } = require('green-parse') | |
const trend = UNICODE.plus().map(chars => chars.join('')) | |
// match e.g. "trend1, trend2, trend3" and return ["trend1", "trend2", "trend3"] | |
const trends = trend.plus(', ') | |
// match e.g. "trend1, trend2 and trend3" and return ["trend1", "trend2", "trend3"] | |
const trendsAndTrend = seq([trends, ' and ', trend]) | |
.map(([trends, and, trend]) => [...trends, trend]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AppScan may flag up a call like this as "Insecure Use of Document.Write" | |
// because it isn't intelligent enough to know that this a call to a totally | |
// unrelated method also named `write` | |
const diffBuffer = PNG.sync.write(diffPng) | |
// Replace with: | |
// eslint-disable-next-line no-useless-call | |
const diffBuffer = PNG.sync.write.call(PNG.sync, diffPng) | |
/////////////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nextafter = require('nextafter') | |
for (let i = -Infinity; i !== Infinity; i = nextafter(i, Infinity)) { | |
console.log(i) | |
} | |
// Whoops, forgot a few the first time I ran this | |
console.log(Infinity) | |
console.log(NaN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <https://twitter.com/Revolvolutionry/status/1165616879009816576> | |
// <https://oeis.org/A036746> | |
const { arabicToRoman, romanToArabic } = require('big-roman') | |
// The entry at index `i` in this array is the smallest Roman numeral | |
// of length `i` | |
const results = [''] | |
const stopAt = 281 | |
let powerOfTen = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// No bounds/type checking | |
// No support for the overbar extension for numbers past 3999 | |
const banks = [ | |
['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'], | |
['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'], | |
['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'], | |
['', 'M', 'MM', 'MMM'] | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(s => console.log(`${s}\n(${JSON.stringify(s)})\n`)) | |
("(s => console.log(`${s}\\n(${JSON.stringify(s)})\\n`))") |
NewerOlder