Skip to content

Instantly share code, notes, and snippets.

View yukidarake's full-sized avatar
🎯
Focusing

Toshiyuki Nakamura yukidarake

🎯
Focusing
View GitHub Profile
@yukidarake
yukidarake / jquerify.js
Created November 2, 2011 04:37
jQueryを使ってないページでjQueryを使用可能にし、何かする
(function() {
var d = document,
s = d.createElement('script');
s.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js';
s.onload = function() {(function($) {
// $('body') ...
})(jQuery)};
d.body.appendChild(s);
@yukidarake
yukidarake / gist:1445883
Created December 8, 2011 02:42
process.nextTickのパフォーマンスを計測
var count = 0;
function countUp() {
count++;
process.nextTick(countUp);
}
countUp();
setInterval(function() {
var c = count;
count = 0;
console.log(c + ' count/s, ' + parseInt(c / 1000) + ' tick/ms');
@yukidarake
yukidarake / date.js
Created December 9, 2011 01:35
時間をyyyy-MM-dd hh:mm:ss.SSSとして表示
function now() {
var d = new Date(),
yyyy = d.getYear() + 1900,
MM = ('0' + (d.getMonth() + 1)).slice(-2),
dd = ('0' + d.getDate()).slice(-2),
hh = ('0' + d.getHours()).slice(-2),
mm = ('0' + d.getMinutes()).slice(-2),
ss = ('0' + d.getSeconds()).slice(-2),
SSS = ('00' + d.getMilliseconds()).slice(-3);
return yyyy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss + '.' + SSS;
@yukidarake
yukidarake / spawn.js
Created December 13, 2011 10:51
optimist で いい感じに spawn させる例
var spawn = require('child_process').spawn;
var argv = require('optimist')
.usage('Usage: $0 -c [num] [cmd]\nExample: $0 -c 4 ls ..')
.demand('c')
.alias('c', 'concurrency')
.argv;
console.time(argv.$0);
for (var i = 1; i <= argv.c; i++) {
@yukidarake
yukidarake / loop.js
Created February 2, 2012 06:48
node.jsとかで重めなループを回すと、イベントループがブロックされるのでいい感じに間にprocess.nextTickを挟んでくれるように
function loop(fn, times, unit) {
unit = unit || 10;
function _loop(n) {
for (var i = n; i < n + unit; i++) {
fn(i);
if (i === times - 1) {
return;
}
}
process.nextTick(function() {
@yukidarake
yukidarake / delay_loop.js
Created February 6, 2012 09:54
node.jsのテストで使えるかと思って出来心で書いてみた。
function loop(fn, times, delay) {
if (!times || times < 0) {
throw new Error('illegal parameter times', times);
}
if (!delay || delay < 0) {
throw new Error('illegal parameter delay', delay);
}
function _loop(n) {
if (n === times) {
return;
@yukidarake
yukidarake / performance.js
Last active September 30, 2015 21:47
node.jsで最適なパフォーマンスを得るための書き方を測定
var Benchmark = require('benchmark');
var options = {
onStart : function() {
console.log('h1. ' + this.name + 'で一番速いのは?');
},
onCycle : function(event, bench) {
console.log('* ' + bench.name + '\t' + bench.hz.toFixed(bench.hz < 100 ? 2 : 0));
},
onComplete : function() {
@yukidarake
yukidarake / gist:2145561
Created March 21, 2012 08:13
Node.jsをバージョンアップした際、npmのモジュールインストールし忘れを防ぐ
for x in optimist async jshint; do
if ! npm ls -g | grep $x > /dev/null; then
npm install -g $x
fi
done
@yukidarake
yukidarake / replace.pl
Created June 18, 2012 11:42
config.hogeというfunctionブロックを除去し、インデントを一つ下げる
use strict;
use warnings;
for my $js(<*.js>) {
open my $new, '>', "$js.2" or die $!;
open my $old, '<', $js or die $!;
my $spaces = '';
while (my $line = <$old>) {
if (!$spaces && (($spaces) = ($line =~ /^(\s+)config.hoge/))) {
@yukidarake
yukidarake / stream_example.js
Created August 18, 2012 08:30
node.jsで標準出力から一行ずつ読み込んでファイルに書きこむ例
var fs = require('fs');
var buffer = '';
var out = fs.createWriteStream('test.txt', {
encoding: 'utf8',
mode: 0644,
});
process.stdin.resume();
process.stdin.setEncoding('utf8');