There's currently String.fromCharCode and String.fromCodePoint in EcmaScript to convert a (sequence of) character code(s)/code point(s) to a String, but this only works if the input codes are passed as parameters to these String builtins. So a common pattern in JavaScript to construct a String from the bytes in an ArrayBuffer (that was read from a file or fetched from some server) is to either use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Micro-benchmark to answer the question in https://twitter.com/thejameskyle/status/905403367949647874 | |
if (typeof console === 'undefined') console = {log:print}; | |
var closuresOriginal = (function() { | |
const outer = a => { | |
const inner = b => a + b; | |
return inner(2); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a = Object.assign({}, {x:1, y:2, z:3}); | |
let b = Object.assign({}, a); | |
console.log("a is", a); | |
console.log("b is", b); | |
console.log("a and b have same map:", %HaveSameMap(a, b)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a = {x:1, y:2, z:3}; | |
let b = Object.assign({}, a); | |
console.log("a is", a); | |
console.log("b is", b); | |
console.log("a and b have same map:", %HaveSameMap(a, b)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a = {x:1, y:2, z:3}; | |
let b = {}; | |
b.x = 1; | |
b.y = 2; | |
b.z = 3; | |
console.log("a is", a); | |
console.log("b is", b); | |
console.log("a and b have same map:", %HaveSameMap(a, b)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const s1 = todo({}, { | |
type: 'ADD_TODO', | |
id: 1, | |
text: "Finish blog post" | |
}); | |
const s2 = todo(s1, { | |
type: 'TOGGLE_TODO', | |
id: 1 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const todo = (state = {}, action) => { | |
switch (action.type) { | |
case 'ADD_TODO': | |
return { | |
id: action.id, | |
text: action.text, | |
completed: false | |
} | |
case 'TOGGLE_TODO': | |
if (state.id !== action.id) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacci(num){ | |
var a = 1, b = 0, temp; | |
while (num >= 0){ | |
temp = a; | |
a = a + b; | |
b = temp; | |
--num; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////////////////////////////////////// | |
// Test framework: | |
// Warmup | |
for (var i = 0; i < 1000; ++i) test(data); | |
function time(test, a) { | |
let startTime = Date.now(); | |
// Using F.p.apply here to prevent inlining, so we can really | |
// just measure the performance of test stand-alone. |