Skip to content

Instantly share code, notes, and snippets.

View justjavac's full-sized avatar
😷

迷渡 justjavac

😷
View GitHub Profile
@justjavac
justjavac / gc-track.js
Last active June 29, 2022 11:43
JS中怎样判断某个对象是否被GC回收? https://www.zhihu.com/question/345974014/answer/826236205
// 需要通过 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 March 31, 2024 06:30
V8 %GetOptimizationStatus

%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:

// 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 September 20, 2017 20:55
Following up on discussion at @munichjs how to create packed arrays pre-initialized in V8
"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 March 20, 2022 12:27
Demo: Coordinating async React with non-React views

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?

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 05:23
keyword super in babel and typescript
"use strict";
class Point {
getX() {
console.log(this.x); // C
}
}
class ColorPoint extends Point {
constructor() {
@gaearon
gaearon / combining.js
Created June 3, 2015 18:03
Combining Stateless Stores
// ------------
// counterStore.js
// ------------
import {
INCREMENT_COUNTER,
DECREMENT_COUNTER
} from '../constants/ActionTypes';
const initialState = { counter: 0 };
@justjavac
justjavac / img2txt.js
Last active December 9, 2021 06:46
img2txt:基于canvas的图片转字符画工具
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 July 21, 2013 14:45
浅谈代码的局部性

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

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

看一段代码:

function aFunc() {
    for(var i = 0; i < 100; i++) {
 doSth(i);