Skip to content

Instantly share code, notes, and snippets.

View spamshaker's full-sized avatar

Michał Grzegorzewski spamshaker

View GitHub Profile
@spamshaker
spamshaker / node-vm-bench.mjs
Created June 22, 2024 04:22
Performs quick bench for running code in vm against main process
import { createContext, runInContext} from 'node:vm';
import { performance } from 'node:perf_hooks';
const fibonacci = n => n <= 2 ? n - 1 : fibonacci(n - 1) + fibonacci(n - 2);
const context = createContext({ fibonacci });
let time;
let result;
@spamshaker
spamshaker / hoisting.mjs
Created April 28, 2024 05:47
How ES6 hoisting works
/**
* Hoisting does not work; The userLicenseFactory method is not hoisted to the top.
* @throws {ReferenceError} ReferenceError: Cannot access 'User' before initialization
*/
// console.log(User);
/**
* Hoisting works; The {UserFunc} method is hoisted to the top.
*/
console.log(UserFunc);
@spamshaker
spamshaker / performance-stress.js
Created October 23, 2023 14:24
performance stress test
'use strict';
const cluster = require('cluster');
const os = require('os');
const now = () => performance.now();
let time = 10000;
let start = now();
let end = start + time;
if (cluster.isMaster) {
let total = 0;
const numCPUs = os.cpus().length;
@spamshaker
spamshaker / base64.mjs
Last active October 14, 2023 09:56
Javascript base64 encode/decode using native Browser/Node methods
// Encodes any text to base64;
export const encodeBase64 = text => btoa(String.fromCodePoint(...Array.from(new TextEncoder().encode(text))));
// Decodes base64 to text;
export const decodeBase64 = text => new TextDecoder().decode(Uint8Array.from(atob(text), (m) => m.codePointAt(0)));
@spamshaker
spamshaker / match-jsdoc-comment.js
Created August 11, 2023 04:11
match-jsdoc-comment.js
`/**
* This is a comment
*/
class MyClass {
/**
* This is a comment
*/
function doSmth(){
const thing = '';
return thing + 'done';
@spamshaker
spamshaker / iframe-jest-jsdom-environment.js
Last active January 22, 2023 19:56
IFrame jest simulation with jest by custom IFrameJSDOMEnvironment
const {default: JSDOMEnvironment} = require('jest-environment-jsdom');
const {JSDOM} = require('jsdom');
const createWindow = (url) => new JSDOM('', {
runScripts: 'dangerously',
url
}).window;
class IFrameJSDOMEnvironment extends JSDOMEnvironment {
constructor(config, context) {