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
// http://jsperf.com/events-object-compare-vs-string-compare/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
function generateUUID() { | |
var d = new Date().getTime() | |
if (window.performance && typeof window.performance.now === "function") { | |
d += window.performance.now() //use high-precision timer if available | |
} | |
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | |
var r = (d + Math.random() * 16) % 16 | 0 | |
d = Math.floor(d / 16) | |
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16) | |
}) |
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
<script> | |
Benchmark.prototype.setup = function() { | |
var emptyFn = function(str, num) {} | |
var str = 'test string' | |
var num = 1 | |
var preBoundFn = emptyFn.bind(null, str, num) | |
}; | |
Benchmark.prototype.teardown = function() { |
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
// approximate implementation of Set using Symbol (ES6) | |
(() => { | |
function aSet() { | |
this.hash = {} | |
} | |
aSet.prototype = { | |
add(a) { this.hash[a] = true }, | |
has(a) { return a in this.hash }, | |
values() { return Object.keys(this.hash) }, |
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
(() => { | |
class Counter { | |
constructor(start_value) { | |
this.plus = this.plus(start_value) | |
} | |
plusFn(num, _num) { | |
var res = num + _num | |
this.plus = (_num) => { | |
return this.plusFn(res, _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
var Stream = require('stream.js') // https://github.com/dionyziz/stream.js | |
var myStream = Stream.make({ | |
key:'please' | |
}, { | |
key: 'filter' | |
}, { | |
key: 'this' | |
}, { | |
key: 'data' |
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
(() => { | |
class BinaryTree { | |
constructor(value, left = null, right = null) { | |
this.value = value | |
this.left = left | |
this.right = right | |
} | |
[Symbol.iterator]() { | |
var tree = [this.value, this.left, this.right] |
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
// Alt 1 | |
Object.prototype.entries = function() { | |
let keys = Reflect.ownKeys(this).values() | |
return { | |
[Symbol.iterator]() { | |
return this | |
}, | |
next: () => { | |
let { done, value: key } = keys.next() |
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
var trace = x => { | |
console.log(x) | |
return x | |
} | |
var plus1 = x => x + 1 | |
var add2 = (x, y) => x + y | |
var addList = list => list.reduce(add2, 0) | |
var concat2 = (x, y) => x.concat(y) | |
var concatLists = (...lists) => lists.reduce(concat2, []) |
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
// gets all html elements with data-key attribute and stores them in a dictonary. | |
// later, using DataBind function triggers a setter function for that key | |
// which applies the assigned value to the associated node.textContent | |
var DataBind = (() => { | |
// private member | |
var _binder = Array.from(document.querySelectorAll('[data-key]')) | |
.reduce((dict, node) => { | |
Object.defineProperty(dict, node.dataset.key, { | |
set(new_value) { | |
// node is in the closure |