Skip to content

Instantly share code, notes, and snippets.

Avatar
🕳️
Building

0xZensh zensh

🕳️
Building
  • aliyun.com
  • Shanghai, China
View GitHub Profile
@zensh
zensh / test_generator_async.js
Last active August 25, 2018 22:12
Generator performance in Node.js v8
View test_generator_async.js
'use strict'
const thunk = require('thunks')()
const co = require('co')
function * test (i) {
return yield Promise.resolve(i)
}
// async function test (i) {
@zensh
zensh / selectors.go
Last active September 28, 2016 09:05
Go lang Selectors
View selectors.go
package main
import "fmt"
type T0 struct {
x int
}
func (i *T0) M0() int {
return i.x * 110
@zensh
zensh / pipeling_request.js
Created July 20, 2016 04:26
Test pipeling request for Koa, Toa and node.js http server.
View pipeling_request.js
'use strict'
const net = require('net')
const thunk = require('thunks')()
const buf = new Buffer('GET / HTTP/1.1\r\nHost: localhost:3333\r\nConnection: keep-alive\r\n\r\n')
thunk(function * () {
var server
console.log('\n=== Http server ===')
server = getHttpServer()
@zensh
zensh / thunk-redis-bench.js
Created May 1, 2015 14:48
thunk-redis-bench.js
View thunk-redis-bench.js
'use strict';
const Thunk = require('thunks')();
const redis = require('../index');
Thunk(function*() {
var timeT = 0;
var testLen = 50000;
var titleT = 'redis(T):';
var clientT = redis.createClient(6379);
@zensh
zensh / test-co.js
Created November 18, 2014 04:52
co v3 VS co v4
View test-co.js
'use strict';
/*global console*/
var len = 1000, times = 1000;
var tasks = [];
function task(callback) {
callback();
}
View catch-err.js
'use strict';
/*global console*/
var co = require('co');
function task(callback) {
callback(null, 1);
};
setImmediate(function () {
@zensh
zensh / demo.js
Last active August 29, 2015 14:02
'Maximum call stack size exceeded' in 'co'
View demo.js
'use strict';
/*global console*/
// co version 3.0.6
var co = require('co');
function task(callback) {
callback(null, 1);
};
@zensh
zensh / FizzBuzzWhizz.js
Created May 3, 2014 02:30
FizzBuzzWhizz
View FizzBuzzWhizz.js
function game(a, b, c) {
for (var i = 1, s = ''; i <= 100; i++) {
s = (i + '').indexOf(a) >= 0 ? 'Fizz' :
(i % a ? '' : 'Fizz') + (i % b ? '' : 'Buzz') + (i % c ? '' : 'Whizz');
console.log(s || i);
};
}
@zensh
zensh / ngPagination.js
Created April 11, 2013 07:01
ngPagination —— a directive for AngularJS
View ngPagination.js
'use strict';
angular.module('app.directives', []).
directive('ngPagination', function () {
// <div ng-pagination="pagination" class="pagination"></div>
// 基于Bootstrap框架,从父作用域继承下面四个属性
// scope.sizePerPage = [25, 50, 100];
// scope.pageSize = 25;
// scope.pageIndex = 1;
// scope.total = 10;
@zensh
zensh / CacheLRU.js
Last active June 26, 2022 09:37
适用于Node.js的一个LRU缓存,capacity为缓存容量,为0时构造一般缓存。
View CacheLRU.js
function CacheLRU(capacity) {
/* 利用Buffer写的一个LRU缓存,capacity为缓存容量,为0时不限容量。
myCache = new CacheLRU(capacity); //构造缓存
myCache.get(key); //读取名为key的缓存值
myCache.put(key, value); //写入名为key的缓存值
myCache.remove(key); //删除名为key的缓存值
myCache.removeAll(); //清空缓存
myCache.info(); //返回myCache缓存信息
LRU原理:对所有缓存数据的key构建hash链表,当对某一数据进行get或put操作时,将其key提到链表前端(最新)。当进行put数据超出容量时,删除链表尾端(最旧)的缓存数据。
hash链表操作可直接定位key,无需历遍整个hash对象,故读写极快。缓存容量不再影响读写速度。