Skip to content

Instantly share code, notes, and snippets.

View Aschen's full-sized avatar
💭
:trollface:

Adrien Maret Aschen

💭
:trollface:
View GitHub Profile
@Aschen
Aschen / README.md
Created July 18, 2022 17:07
For loop with `var` vs `let`

For loop with var vs let

Using a var instead of a let in a for loop was a game changer with Node.js 6 but now it's merely the same performances.

for (var i = 0; i < 10000; i++) {
  // ...
}

for (let i = 0; i < 10000; i++) {
@Aschen
Aschen / README.md
Created May 11, 2022 17:39
Call function with simple argument vs object argument

npm i benchmark

results

simple function call x 962,129 ops/sec ±2.62% (79 runs sampled)
object function call x 900,959 ops/sec ±2.13% (89 runs sampled)
Fastest is simple function call

Around 0.1 ms for 1000 calls

@Aschen
Aschen / README.md
Last active April 12, 2022 15:20
Benchmark mutable vs immutable for objects and arrays

Immutability has a cost, here is how much

object - vanilla immutable x 903,841 ops/sec ±5.33% (77 runs sampled)
object - immutable.js    x 1,672,774 ops/sec ±1.68% (89 runs sampled)
object - mutable       x 546,235,424 ops/sec ±0.84% (83 runs sampled)

array - vanilla immutable x 4,855,761 ops/sec ±1.55% (91 runs sampled)
array - immutable.js        x 618,146 ops/sec ±3.56% (85 runs sampled)
array - mutable x 9,849,954 ops/sec ±4.00% (85 runs sampled)
@Aschen
Aschen / clone-benchmark.js
Created March 16, 2022 03:59
Cloning Javascript Objects
const {Benchmark} = require('benchmark');
var suite = new Benchmark.Suite;
const justClone = require('just-clone');
const fastClone = require('fast-clone');
const lodashClone = require('lodash').cloneDeep;
const stringifyParse = o => JSON.parse(JSON.stringify(o));
const smallObject = {
name: 'Aschen',
@Aschen
Aschen / README.md
Created March 7, 2022 09:27
Migrate Elasticsearch stored scripts

Migrate Elasticsearch stored scripts

This is a standalone migration script to export and imports Elasticsearch Stored Scripts.

Usage

Export scripts from your ES cluster:

node migrate.js export http://localhost:9200
@Aschen
Aschen / reflect.js
Created January 29, 2022 15:18
Reflect.defineProperty
/**
$ node node/reflect.js
Reflect x 3,102,490 ops/sec ±5.31% (78 runs sampled)
Normal x 61,204,978 ops/sec ±5.73% (86 runs sampled)
Fastest is Normal
*/
const {Benchmark} = require('benchmark');
var suite = new Benchmark.Suite;
@Aschen
Aschen / mono-vs-poly.js
Created January 26, 2022 08:38
Monomorphic vs Polymorphic function
const {Benchmark} = require('benchmark');
var suite = new Benchmark.Suite;
function monomorphic (person) {
const array = [];
for (let i = 10; i--; ) {
array.push(person.age * i);
}
@Aschen
Aschen / README.md
Created November 5, 2021 11:44
Redis: Count, print or delete keys that expires after a specified TTL

Usage:

  • redis-cli --eval clean-keys.lua <mode> <days>
  • redis-cli --eval clean-keys.lua count 1
  • redis-cli --eval clean-keys.lua print 1
  • redis-cli --eval clean-keys.lua delete 1

For example, redis-cli --eval clean-keys.lua count 2 will return the number of keys expiring in more than 2 days

@Aschen
Aschen / expose-admin-console.ts
Created November 1, 2021 15:42
Expose Admin Console in a Kuzzle controller
app.controller.register('redirect', {
actions: {
proxy: {
handler: async request => {
const { css, js, img, fonts } = request.input.args;
let url;
let contentType;
if (css) {
url = `http://console.kuzzle.io/css/${css}`
@Aschen
Aschen / pipe-restrict-index-list.js
Created September 18, 2020 12:01
Restrict index and collection list to user who are allowed to act on it
class KuzzlePlugin {
init (config, context) {
this.pipes = {
'index:afterList': 'restrictIndexes',
'collection:afterList': 'restrictCollections'
};
}