最壞的時代,也是最好的時代
I've got to find what I love
Recently I am thinking how to master javascript performance
I noticed several discussions about recursive and iteration. Fibonacci! (yes, super boring recursive examples but it works quite well for lots of students)
I tried to implement it in javascript and run it in V8 engine with node-bench
Model: MBA
CPU: 1.86G C2D
RAM: 2G
formula
Raw:
> 7153.846153846154
> 7283.716283716284
> 7059.94005994006
> 7017.982017982018
> 7074.925074925075
Average (mean) 7118.081918081918
iteration
Raw:
> 1230.7692307692307
> 1243.7562437562437
> 1237.7622377622379
> 794.2057942057942
> 1237.7622377622379
Average (mean) 1148.851148851149
recursive
Raw:
> 0.03229348317509526
> 0.03268080656230596
> 0.032544667556220916
> 0.03117401334247771
> 0.03111581305619516
Average (mean) 0.031961756738458996
Above results doesn't shock me but it does REMIND me that Formula is always a good point to deal with calculation
shorthand
// style element
var d = document,
h = d.getElementsByTagName('head')[0],
s = d.createElement('link');
s.rel = "stylesheet";
s.href = "css-URL";
s.type = "text/css";
h.appendChild(s);
one liner
var d=document,h=d.getElementsByTagName('head')[0],s=d.createElement('link');s.rel="stylesheet";s.href="css-
URL";s.type="text/css";h.appendChild(s);
// script element
var d = document,
h = d.getElementsByTagName('head')[0],
s = d.createElement('script');
s.href = "js-URL";
h.appendChild(s);
one liner
var d=document,h=d.getElementsByTagName('head')[0],s=d.createElement('script');s.href="js-URL";h.appendChild(s);
It's not bad to read markdown document in terminal, however, it's better if we do have another choice, especially it's free and beautiful
Readdown is a good alternative to read markdown document. Unfortunately, it's a little buggy? in case you open a .md or extension other than markdown/mkdn/text/txt, it just throw out an error.
Workaround:
To add more extensions to be supported in Readdown
> vim /Applications/Readown.app/Contents/Info.plist
# update below section
<key>CFBundleTypeExtensions</key>
<array>
<string>markdown</string>
<string>md</string>
<string>mkdn</string>
<string>text</string>
<string>txt</string>
</array>
This post is shooting for nodejs-powered Mac users
It's pretty useful if we can shorten URL in command-line, especially we can have a helper to achieve below
1. ease to have/install
2. ease to use
for installation
npm-g install goo.gl
we still need do some modification since goo.gl output key -> value, that say if you do shorten google.com you will see
http://google.com -> http://goo.gl/mR2d
edit /usr/local/bin/goo.gl (check installation path)
change line 135 from
console.log('%s -> %s', val, (res.id || JSON.stringify(res)));
to
console.log('%s', (res.id || JSON.stringify(res)));
added a bash helper in ~/.profile
function gg() {
goo.gl $1 | pbcopy
}
then
source ~/.profile
now you can simply type
gg www.yahoo.com and get shorten url in clipboard
Labels: Javascript, nodejs
YQL is a killer tool to crawl web data, we can fetch remote pages and get json/jsonp/xml output via YQL console.
The magic behind it's using xpath like we crawl web in tranditional way.
However, to be a frontend engineer, to "select" desired DOM elements is a dead simple things via CSS selector and that's what Dav's talk inspire me a lot.
For details about node.js I strongly recommend to read it here: http://nodejs.org
To me it's so interesting to deal w/ DOM stuff at server side. YUI3 (and jsdom) enpower NodeJS fetch pages in a easy(maybe frontend-friendly :P) way.
2. To crawl lady gaga information title in yahoo search
http://hapi.nodester.com/api?host=http://search.yahoo.com/search?p=lady+gaga&fr=sfp&fr2=&iscqry=&rule=%23web%20.res%20.sm-media%20img&callback=ladygaga
3. To crawl iOS/Android app and get all app icon images in yahoo app search
http://hapi.nodester.com/api?host=http://apps.search.yahoo.com/search?p=plants&fr=apps_sfp&fr2=&iscqry=&rule=%23main%20.app-res%20.left%20img&callback=yappsearch
To link above you can get a jsonp object and use it in your webpage
There are still a lot of rooms to improve this prototype,
in case some websites needed additional http headers such as Referer or Cookie is not yet supported.
Moreover, to avoid of confusion we have to encode '#' (sharp?) to %23 since it's a valid character in URL and it's a identifier selector in css rule.
1. Dav Glass — Using Node.js and YUI 3
http://developer.yahoo.com/yui/theater/video.php?v=glass-node
2. Dav Glass — Node.js + YUI 3 (YUIConf 2010)
http://developer.yahoo.com/yui/theater/video.php?v=yuiconf2010-glass
Notes about Javascript Patters Chapter 4: Function
named function expression
// named function expression
var add = function add(a, b) {
return a + b;
};
// function expression, a.k.a. anonymous function
var add = function (a, b) {
return a + b;
};
The only difference of above 2 function expressions are name property of function
// named function expression
var add = function add(a, b) {
return a + b;
};
> add.name
"add"
// function expression, a.k.a. anonymous function
var add = function (a, b) {
return a + b;
};
> add.name
""
// function declarations
function foo() {
// function body goes here
}
The difference between function expressions and function declarations are not that obvious, function expressions need tailing semicolon while function declarations don't
Labels: Javascript, js
By reading Javascript Patterns (by Stoyan Stefanov)
> var a = NaN;
undefined
> a.constructor
function Number() { [native code] }
it's so weird that NaN's constructor is in Number type.
> var a = new Array(1, 2, 3);
undefined
> a
[1, 2, 3]
> var b = new Array(1);
undefined
> b
[]
By using Array constructor w/ only one argument, it's array length, not first element.
Conclusion: using Array literal instead of Array constructor
Labels: Javascript, js