Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
I may be slow to respond.

Yan Qing zensh

💭
I may be slow to respond.
View GitHub Profile
@zensh
zensh / test_generator_async.js
Last active Aug 25, 2018
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 Sep 28, 2016
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 Jul 20, 2016
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
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 Nov 18, 2014
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 Aug 29, 2015
'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);
};
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 Apr 11, 2013
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 Mar 7, 2018
适用于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对象,故读写极快。缓存容量不再影响读写速度。
You can’t perform that action at this time.