Skip to content

Instantly share code, notes, and snippets.

Avatar

Timo Tijhof Krinkle

View GitHub Profile
@Krinkle
Krinkle / crypto-aes-gcm.js
Created May 26, 2021 — forked from chrisveness/crypto-aes-gcm.js
Uses the SubtleCrypto interface of the Web Cryptography API to encrypt and decrypt text using AES-GCM (AES Galois counter mode).
View crypto-aes-gcm.js
/**
* Encrypts plaintext using AES-GCM with supplied password, for decryption with aesGcmDecrypt().
* (c) Chris Veness MIT Licence
*
* @param {String} plaintext - Plaintext to be encrypted.
* @param {String} password - Password to use to encrypt plaintext.
* @returns {String} Encrypted ciphertext.
*
* @example
* const ciphertext = await aesGcmEncrypt('my secret text', 'pw');
@Krinkle
Krinkle / 01-parkers-number-of-feb2014.js
Created Apr 18, 2019
3435 – The sum of each digit raised to the power of itself.
View 01-parkers-number-of-feb2014.js
const MAX = 100 * 1000 * 1000; // 100 million
function powself(number) {
if (number === 0) {
return NaN; // matt does not like zero.
} else {
return Math.pow(number, number);
}
}
View 01-cluster-worker-2020.js
/*! Requires Node 12.5+ | License: Public domain. */
const cluster = require('cluster');
const WORK_TOTAL = 10_000_000;
const WORK_MILESTONE = 1_000_000;
const WORK_ASSIGN_CHUNK = 100_000;
const WORK_RESP_CHUNK = 1_000;
const ALGO = 'md5';
@Krinkle
Krinkle / 01-bench_mw-loader-register.js
Last active Feb 9, 2019
This gist is referred to in mediawiki-core.git:/resources/src/startup.
View 01-bench_mw-loader-register.js
// Node.js and browser
var perf = typeof performance !== 'undefined' ? performance : require('perf_hooks').performance;
var registry;
var i;
var start;
var diff;
var count = 10000;
// Baseline: Multi-purpose (one and multi), self-call to unbatch
@Krinkle
Krinkle / perfect-numbers.js
Last active Mar 18, 2019
Distribute exponential task sizes via Node.js cluster workers.
View perfect-numbers.js
/**
* Requires Node 6.0+
*
* Author: Timo Tijhof (2018).
* License: Public domain.
*/
const cluster = require('cluster');
let pidLabel = cluster.isMaster ? 'master' : 'worker';
@Krinkle
Krinkle / 01-search-all-grafana.js
Last active Oct 14, 2018
Search Grafana dashboard JSON
View 01-search-all-grafana.js
const path = require('path');
const req = require('request-promise');
const urlListQuery = 'https://grafana.wikimedia.org/api/search?query=&starred=false';
const urlTplDashQuery = 'https://grafana.wikimedia.org/api/dashboards/$1';
const pattern = process.argv[2];
if (!pattern) {
const cmd = path.basename(process.argv[0]);
const base = path.basename(process.argv[1]);
View cost.js
/**
* https://tfl.gov.uk/corporate/terms-and-conditions/tfl-call-charges
*
* > The rate is 0.66p per minute.
* > There is also a 40p connection charge.
* > Call charges are rounded up to the nearest 10p, with a minimum charge of 60p.
*
* @param {number} seconds
* @return {string} Cost in GBP
*/
View 02-2015-by-month.txt
335709 Apr/2014
634580 May/2014
472835 Jun/2014
557525 Jul/2014
385136 Aug/2014
723163 Sep/2014
738108 Oct/2014
1233766 Nov/2014
1241514 Dec/2014
1072986 Jan/2015