Skip to content

Instantly share code, notes, and snippets.

View justjavac's full-sized avatar
😷

迷渡 justjavac

😷
View GitHub Profile
// 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) {
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() {
@gr2m
gr2m / control_devices.dreamcode.js
Last active August 29, 2016 09:01
Imagine you could control other devices with a simple JavaScript API, right from your browser.
// Ask the coffee machine at IP 192.168.2.2 to do its job
device('192.168.2.2')
.do( 'coffee' )
.then( wakeMeUpCallback )
// turn all lights on
device.findAll('light').do('turnOn')
@gr2m
gr2m / convert_dreamcode.js
Last active August 29, 2016 09:01
Imagine you could convert all kind of things to all kind of different things, e.g. taking a screenshot of a dom element or converting another website to a screenshot. Forks & comments much appreciated! #nobackend #dreamcode
// convert a dom element to a PDF and download it
convert( $('.invoice') ).to( 'invoice.pdf' ).download()
// alternatively
download( convert( $('.invoice') ).to( 'invoice.pdf' ) )
// convert another website to a png and show it on the page
convert( 'http://exam.pl/page' ).toImage().then( $('.screenshots').append )
// attach a file to an email
@gr2m
gr2m / share_dreamcode.js
Last active August 29, 2016 09:01
Imagine you could share user data with JavaScript right in the browser. How would the code look like? This is what I came up with. Forks & comments much appreciated! (see also: https://gist.github.com/gr2m/5463475) #nobackend #dreamcode
// add a new share
share = new Share()
// grant / revoke access
share.grantReadAccess()
share.grantWriteAccess()
share.revokeReadAccess()
share.revokeWriteAccess()
share.grantReadAccess('joe@example.com')
share.revomeWriteAccess(['joe@example.com', 'lisa@example.com'])