Skip to content

Instantly share code, notes, and snippets.

View alsotang's full-sized avatar

alsotang alsotang

View GitHub Profile
@alsotang
alsotang / benchmark.js
Last active April 21, 2016 15:52
Node.js crypto benchmark
var hasha = require('hasha')
var utility = require('utility')
var fs = require('fs')
var str = fs.readFileSync(__filename)
suite('md5', function () {
bench('utility.md5', function() {
utility.md5(str);
@alsotang
alsotang / test.js
Created February 3, 2016 19:56
Node.js vs Python
var _ = require('lodash')
var length = 20000;
var arr = _.range(0, length);
console.time('add')
for (var i = 0; i < length; i++) {
for (var j = 0; j < length; j++) {
arr[j] += 1
var _ = require('lodash')
var base = 10000;
function win() {
return ~~(Math.random() * 10000 % 2)
}
function bet(num) {
@alsotang
alsotang / alock.js
Last active October 10, 2016 07:37
alock
// alock 是用来控制某个函数的回调一定只执行最新的库
var alock = {
count: 0,
locks: {
},
set: function (lockName, val) {
if (!val) {
val = ++this.count
}
@alsotang
alsotang / my_custom_error.js
Created November 5, 2015 06:37
inherit from Error
function MyCustomError(message) {
this.message = message;
this.name = "MyCustomError";
Error.captureStackTrace(this, MyCustomError);
}
MyCustomError.prototype = Object.create(Error.prototype);
MyCustomError.prototype.constructor = MyCustomError;
var cluster = require('cluster');
if (cluster.isMaster) {
for (var i = 0; i < 4; i++) {
setTimeout(function () {
cluster.fork();
}, i * 2 * 1000)
}
} else {
@alsotang
alsotang / 1.js
Last active October 15, 2015 07:25
requestIdleCallback 与前端 cpu 运算
var count = 0;
setInterval(function () {
count++;
var el = document.querySelector('.count-number')
el.innerHTML = count;
}, 100)
// add 10k elements to body
setTimeout(function () {
for (var i = 0; i < 10000; i++) {
@alsotang
alsotang / gist:8452415
Created January 16, 2014 09:55
The run time of the three tests are almost the same.
var should = require('should');
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();
var lambdaLifting = function () {
var x = 10;
function fn() {
return x + 1;
}
@alsotang
alsotang / gist:7319095
Created November 5, 2013 13:36
男女比例
var _ = require('lodash');
var families = [];
for (var i = 0; i < 100000; i++) {
var family = [];
for (var j = 0; j < 3; j++ ) {
// 0 是女孩 1 是男孩
var gender = Math.random() >= 0.5 ? 1 : 0;
family.push(gender);
@alsotang
alsotang / hello.md
Last active December 17, 2015 10:49
just a markdown gist test

Hello World

add some words.

more words here...