Skip to content

Instantly share code, notes, and snippets.

Avatar
😷

迷渡 justjavac

😷
View GitHub Profile
View gc-track.js
// 需要通过 node --allow-natives-syntax 参数运行
const { GCSignal, consumeSignals, trackGarbageCollection } = require("gc-signals");
function fn() {
// 创建两个局部变量
const a1 = new GCSignal(1);
const a2 = new GCSignal(2);
global.tmp = a2; // 将 a2 变量赋值给全局变量
const o = {};
trackGarbageCollection(o, 3); // 跟踪局部变量 o 的 GC 状态,标识为 3
@justjavac
justjavac / GetOptimizationStatus.md
Last active Oct 18, 2020
V8 %GetOptimizationStatus
View GetOptimizationStatus.md

%GetOptimizationStatus return a set of bitwise flags instead of a single value, to access the value, you need to take the binary representation of the returned value. Now, for example, if 65 is returned, the binary representation is the following:

(65).toString(2).padStart(12, '0');
// 000001000001

Each binary digit acts as a boolean with the following meaning:

View unicode.js
// JavaScript 字符串编码使用 UTF-16
"💩".length === 2;
"💩" === "\u{1F4A9}"; // es6
"💩" === "\uD83D\uDCA9"; // es5
// 同一个编码可能使用不同的码位
"ò" === "ò"; // ❎
"ò" === "\xF2"; // ✅
"ò" === "o\u0300"; // ✅
"o\u0300".normalize("NFC") === "\xF2"; // ✅ es6 提供了 normalize 函数
@bmeurer
bmeurer / mathias.js
Created Sep 20, 2017
Following up on discussion at @munichjs how to create packed arrays pre-initialized in V8
View mathias.js
"use strict";
// These are examples how to created PACKED_*_ELEMENTS arrays preinitialized
// in V8, following up on offline discussion at the last MunichJS meetup.
function createPackedViaArrayFrom(length, value) {
return Array.from.call(null, Array.prototype.map.call({length}, _ => value));
}
function createPackedViaGenerator(length, value) {
@acdlite
acdlite / coordinating-async-react.md
Last active Feb 15, 2020
Demo: Coordinating async React with non-React views
View coordinating-async-react.md

Demo: Coordinating async React with non-React views

tl;dr I built a demo illustrating what it might look like to add async rendering to Facebook's commenting interface, while ensuring it appears on the screen simultaneous to the server-rendered story.

A key benefit of async rendering is that large updates don't block the main thread; instead, the work is spread out and performed during idle periods using cooperative scheduling.

But once you make something async, you introduce the possibility that things may appear on the screen at separate times. Especially when you're dealing with multiple UI frameworks, as is often the case at Facebook.

How do we solve this with React?

View typedarray.js
function test() {
const a = new Int32Array(10);
for (let i = 0; i < 1e6; i++) {
a.set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
return a;
}
console.time('test');
test();
@justjavac
justjavac / super-in-babel-typescript.js
Last active May 24, 2017
keyword super in babel and typescript
View super-in-babel-typescript.js
"use strict";
class Point {
getX() {
console.log(this.x); // C
}
}
class ColorPoint extends Point {
constructor() {
@gaearon
gaearon / combining.js
Created Jun 3, 2015
Combining Stateless Stores
View combining.js
// ------------
// counterStore.js
// ------------
import {
INCREMENT_COUNTER,
DECREMENT_COUNTER
} from '../constants/ActionTypes';
const initialState = { counter: 0 };
@justjavac
justjavac / img2txt.js
Last active Sep 9, 2020
img2txt:基于canvas的图片转字符画工具
View img2txt.js
var cv = document.getElementById('cv');
var c = cv.getContext('2d');
var txtDiv = document.getElementById('txt');
var fileBtn = document.getElementById("up-button");
var img = new Image();
img.src = 'a.jpg';
img.onload = init; // 图片加载完开始转换
fileBtn.onchange = getImg;
// 根据灰度生成相应字符
@wintercn
wintercn / local.md
Created Jul 21, 2013
浅谈代码的局部性
View local.md

我是JS中"就近声明"以及"允许多次var声明"的拥护者。

我持这样观点源自我对易读性的追求。

看一段代码:

function aFunc() {
    for(var i = 0; i < 100; i++) {
        doSth(i);
You can’t perform that action at this time.