Skip to content

Instantly share code, notes, and snippets.

View alsotang's full-sized avatar

alsotang alsotang

View GitHub Profile
var cluster = require('cluster');
if (cluster.isMaster) {
for (var i = 0; i < 4; i++) {
setTimeout(function () {
cluster.fork();
}, i * 2 * 1000)
}
} else {
@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;
@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
}
var _ = require('lodash')
var base = 10000;
function win() {
return ~~(Math.random() * 10000 % 2)
}
function bet(num) {
@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
@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 / 提问的智慧.md
Created August 6, 2016 04:51 — forked from zer4tul/提问的智慧.md
Simplified Chinese edition of "How To Ask Questions The Smart Way"
function forceInterval(fn, time) {
var lastRunTime = new Date();
return setInterval(function () {
var currentTime = new Date()
if ((new Date - lastRunTime) < time) {
fn();
} else {
var count = ~~((new Date - lastRunTime) / time)
for (var i = 0; i < count; i++) {
while true
do
sleep 1
wget -qO- http://107.170.206.53/hk1
done
@alsotang
alsotang / diff.js
Created August 26, 2018 18:46
diff two text with enhanced inline diff
require('colors');
const jsdiff = require('diff')
const stringSimilarity = require('string-similarity');
const text1 = `
{
"name": "calcit-editor",
"version": "0.3.50",
"description": "Cirru Calcit Editor",
"bin": {
"calcit-editor": "dist/server.js",