Skip to content

Instantly share code, notes, and snippets.

@Grassboy
Grassboy / .js
Created June 26, 2019 08:14
which_is_faster.js
console.time('p1');
var data, n = 1000000;
for(var i = 0 ; i < n; i ++) data = { foo: 123, bar: i };
console.timeEnd('p1');
console.time('p2');
for(var i = 0 ; i < n; i ++) data = JSON.parse("{\"foo\":123,\"bar\":"+i+"}");
console.timeEnd('p2');
//p1: 872ms - 計時器停止
//p2: 1900ms - 計時器停止
var valid_list = [];
var diffCount = function(a, b) {
var result = 0;
if(parseInt(a/100000)%10 != parseInt(b/100000)%10) result++;
if(parseInt(a/10000)%10 != parseInt(b/10000)%10) result++;
if(parseInt(a/1000)%10 != parseInt(b/1000)%10) result++;
if(parseInt(a/100)%10 != parseInt(b/100)%10) result++;
if(parseInt(a/10)%10 != parseInt(b/10)%10) result++;
if(parseInt(a/1)%10 != parseInt(b/1)%10) result++;
return result;
@Grassboy
Grassboy / jQuery.node.example.js
Created August 23, 2016 17:29
Node.js 下的 jQuery 起手式範例(ajax已可跨網域)
var jsdom = require('jsdom');
var $ = require('jquery')(jsdom.jsdom('<html></html>').defaultView);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
parseHTML = function(html){
//this extract body function may have a bug
var html = '<body'+html.split('<body')[1].split('</body>')[0]+'</body>'
return $(html);
};
//{{ PromisePool
/* Usage:
var promise_pool = new PromisePool([concurrency_limit]);
* concurrency_limit: n of concurrency promices executions
generate a PromisePool
promise_pool.push(fn);
* function: the function to generate promise object
push a function into PromisePool, this functipn will generate promise object
but the real Promise object has *NOT* been generated yet
//{{ PromisePool
/* Usage:
var promise_pool = new PromisePool([concurrency_limit]);
* concurrency_limit: n of concurrency promices executions
generate a PromisePool
promise_pool.push(function, args);
* function: the function to generate promise object
* args: the arguments array of function
push a promise object into PromisePool, but the real Promise object has *NOT* been generated yet
@Grassboy
Grassboy / getLikesTopN.js
Created February 25, 2016 11:07
取得臉書所有照片按讚數排名
//取得自己牆上按讚數排行
/*
使用方式,
0. 安裝 gClipboard: https://addons.mozilla.org/zh-TW/firefox/addon/grassboy-clipboard/
1. 到 https://www.facebook.com/[你的臉書id]/photos_all 進入你的相片頁,並捲開所有相片
2. Firefox 下按 Shift+F4
3. 貼入這整段文字
4. 頁面會將結果複製到剪貼簿裡
5. 貼到記事本,就是你所有相片的按讚數排名了~
*/
//Get the dataURL of Endomondo's Map Path
//requirement: jQuery
javascript: (function() {
var input = $('#wokoutMapContainer .gm-style div:first div:first')[0];
var final = {
x1: Number.MAX_VALUE,
y1: Number.MAX_VALUE,
x2: Number.MIN_VALUE,
y2: Number.MIN_VALUE
};
@Grassboy
Grassboy / gDataURL.js
Last active October 29, 2015 03:10
get the dataURL of specific url
var gDataURL = function(opts){
opts = opts || {};
opts.onload = opts.onload || function(r){
console.log(r);
};
opts.url = opts.url || location.href;
var oReq = new XMLHttpRequest();
oReq.open("GET", opts.url, true);
oReq.responseType = "blob";
Handler function threw an exception: [Exception... "Component returned failure code: 0x80004002 (NS_NOINTERFACE) [nsIWebProgress.DOMWindow]" nsresult: "0x80004002 (NS_NOINTERFACE)" location: "JS frame :: resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/actors/webbrowser.js :: TabActor.prototype._docShellsToWindows/< :: line 1132" data: no]
Stack: TabActor.prototype._docShellsToWindows/<@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/actors/webbrowser.js:1132:11
TabActor.prototype._docShellsToWindows@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/actors/webbrowser.js:1129:1
TabActor.prototype._notifyDocShellsUpdate@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/actors/webbrowser.js:1155:19
TabActor.prototype._onDocShellCreated/<@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/devtools/server/actors/webbro
var div = $('<div class="item" data-foo="bar">');
alert(div.data('foo'));
div.attr('data-foo', 'barbarbar');
alert(div.data('foo'));