A mostly reasonable approach to JavaScript
This document has been modified from its [original format][m1], which was written by Ning Shang ([email protected]). It has been updated and reformatted into a [Markdown][m2] document by [Woody Gilk][m3] and [republished][m4].
When working with a remote git repository which is hosted on a third-party storage server, data confidentiality sometimes becomes
| // find out if a DOM element is within the viewport top/bottom | |
| var ele = // some DOM element | |
| function(){ | |
| var bodyHeight = document.body.getBoundingClientRect().height; | |
| var rect = ele.getBoundingClientRect(); | |
| if(rect.bottom > 0 && rect.top < bodyHeight){ | |
| console.log('in'); | |
| } else { | |
| console.log('out'); |
| DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
| Version 2, December 2014 | |
| Copyright (C) 2014 Addy Osmani @addyosmani | |
| Everyone is permitted to copy and distribute verbatim or modified | |
| copies of this license document, and changing it is allowed as long | |
| as the name is changed. | |
| DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <p>hi</p> | |
| <svg xmlns="http://www.w3.org/2000/svg" height="98" width="581" viewBox="0 0 581 98"><path d="M62.9 14.9c-25-7.74-56.6 4.8-60.4 24.3-3.73 19.6 21.6 35 39.6 37.6 42.8 6.2 72.9-53.4 116-58.9 65-18.2 191 101 215 28.8 5-16.7-7-49.1-34-44-34 11.5-31 46.5-14 69.3 9.38 12.6 24.2 20.6 39.8 22.9 91.4 9.05 102-98.9 176-86.7 18.8 3.81 33 17.3 36.7 34.6 2.01 10.2.124 21.1-5.18 30.1" stroke="#000" stroke-width="4.3" fill="none" stroke-dasharray="298.87 298.87" stroke-dashoffset="980.95"></path></svg> | |
| <script id="jsbin-javascript"> |
When you get the website on your browser, initially there is nothing to see. hack.ernews.info uses the HN Firebase API, so once the site is loaded, it has to make a websocket connection to Firebase ( can be really slow on some countries/connections ) and once it does, it loads the ids of the top stories, and then, it has to get info on each one of them, and then react can render the stories.
What hack.ernews.info does, is that apart from the main webserver, there is a pre-renderer server, that runs the "website" on the server, meaning that at any given point in time, it has the latest "top stories". Every 1 second, that pre-renderer sends the "#main-app" HTML content to the main server, which keeps it, injects it in a placeholder in the index.html, and React on the client knows that it won't touch it, until it loads stories.
So the user gets the index.html with the markup from the pre-renderer, and once firebase websocket has delivered all the data, react does it's magic and it's as
| var emailRegExp = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; |
Here's all you have to do to add clustering to your node.js application.
- save this code as
cluster.js, and runcluster.jsinstead ofserver.js(or /bin/www, or whatever it's called for your project) - the only line you'll need to change is the last line - it needs to point to the location of your
server.jsfile
var cluster = require('cluster');
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;bash-3.2$ node -v && sh ./run.sh
v0.10.36
Version 1
Called v1, util.yo("man") 1.000.000 times in 0.005742424 sec
Called v1, util.yo("man") 10.000.000 times in 0.046646901 sec
Called v1, util.yo("man") 100.000.000 times in 0.461483307 sec
Called v1, util.yo("man") 1.000.000.000 times in 4.760366051 sec
Version 2
Called v2, util.yo("man") 1.000.000 times in 0.005232417 sec