Skip to content

Instantly share code, notes, and snippets.

@hax
hax / test-block-scope-performance.js
Created April 30, 2016 08:46
block scope optimization test
'use strict'
const Benchmark = require('benchmark')
function MASK() {
return 100
}
function MAX() {
@hax
hax / index.js
Created December 7, 2016 19:32
log with calling location info in Node.js
const {main} = require('./main')
main()
// See https://github.com/nodejs/node/issues/6456
import {platform} from 'os'
import {satisfies} from 'semver'
if (platform() === 'darwin' && satisfies(process.version, '>=6.0.0 <6.2.1')) {
for (const s of [process.stdout, process.stderr]) {
if (s && s.isTTY && s._handle && typeof s._handle.setBlocking === 'function') {
s._handle.setBlocking(true)
}
@hax
hax / readonly.js
Created December 4, 2018 10:32
readonly collections
void function () {
'use strict'
const targetSymbol = Symbol()
function createSafeMethod(method) {
return new Proxy(method, {
apply(target, thisArg, argArray) {
return Reflect.apply(target, thisArg[targetSymbol], argArray)
}
@hax
hax / class-fields-shanghai-meeting.md
Last active September 1, 2019 18:53
class fields 提案闭门研讨会(上海,2019年9月)
@hax
hax / iterator-with-message.md
Last active April 26, 2024 04:51
An idea from combination of `[...rest, last]` syntax, reverse iterator and `function.sent`
@hax
hax / json-slashs-hint.md
Last active September 23, 2020 07:01
JSON `\//` Hint

JSON \// Hint

Problems

JSON.stringify({x: 1234567890n}) // throw!

You can custom it

@hax
hax / promise-trans.md
Last active May 19, 2020 04:00
Promise 相关译名考虑

译名方法:

  • 黑话型(生造新词)
  • 会意型(合成新词、古词新用)
  • 假借型(复用引申已有专用名词)
  • 白话型(直接用日常用语)

术语

  • Promise
  • Status/State: pending/fulfilled/rejected, settled
  • Fate: resolved/unresolved
// 根据 https://www.zhihu.com/question/391388615/answer/1193135808 中的代码重构,
// 以对应所 fork 的工业聚版本
// 和工业聚版本的主要区别:
// - OO 范式而不是 FP 范式(其实像知乎原版那样不用 class 写成 makeTree 函数也是可以的,这里
// 主要是响应工业聚的号召,更「class」一些。但因为是 JS 而不是 TS 写的,就没有再加额外的
// interface 或 abstract class 了。)
// - Tree 没有弄成 immutable 的。要弄成 immutable 也不是不行,但 OO 范式下处理这类数据结构,
// 通常不会特意弄成 immutable 的,而是在必要的时候提供 clone 方法。
// - 简易起见,没有对 root 节点做额外处理(实际上似乎也不应该做原本格式的数据必然只有一个
@hax
hax / channel.js
Created May 13, 2020 15:12
A experimental Channel in JS
// kotlin channel: https://kotlinlang.org/docs/reference/coroutines/channels.html
// golang channel
class Channel {
constructor() {
this._s = null
this._r = null
}
send(v) {
if (this._r) {