Skip to content

Instantly share code, notes, and snippets.

View adohe-zz's full-sized avatar

TonyAdo adohe-zz

View GitHub Profile
@adohe-zz
adohe-zz / functions.js
Created February 23, 2014 14:00
some useful piece of js code
//All this following function expression will
//throw the refrence error, which will raise
//when de-referencing an invalid reference
(function foo() {
console.log('hello world');
})();
//foo();
!function foo() {
console.log('beep boop');
@adohe-zz
adohe-zz / gist:9117806
Last active August 29, 2015 13:56
some useful piece of code in javascript
//remove array element function
//JavaScript 1.8
function RemoveElement(array, element) {
!!let (pos = array.lastIndexof(element)) pos != -1 && array.slice(pos, 1);
}
var array = [1, 2, 3, 4, 5];
var pos = array.indexOf(2);
pos > -1 && array.slice(pos, 1);
@adohe-zz
adohe-zz / Inner
Created February 20, 2014 16:18
inner class in java
public final class Outer {
//The anonymous inner class
Object anonymous = new Object() {};
{
class Local{}
Local l = new Local();
}
Outer() {
@adohe-zz
adohe-zz / Cr
Created February 20, 2014 05:44
compile & runtime
//The following cases occur during the compile time
public final class Cr {
//Constant folding
static final int num1 = 1;
static final int num2 = 2;
static int num3 = 3;
static int num4 = 4;
//Method overload
void func(String s) {
@adohe-zz
adohe-zz / gist:9105755
Last active August 29, 2015 13:56
concurrent connections vs connections per second
concurrent connections and connection per second are two different beasts.
To handle 1m concurrent connections a traditional synchronous server would
create 1m threads (or worst, processes) with all the stacks and memory needed.
Node handles that in a sngle thread, but the more connections there are, the
bigger latency per request is need to handle them. because it's single-threaded,
you can however utilize the cluster module to use more than one cpu core on your
machine.
But to handle 1m connections per second, this is about latency, and this you
can only get for the price of higher resource usage. cluster module, horizontal
@adohe-zz
adohe-zz / gist:9095401
Created February 19, 2014 16:18
Object related information in javascript
//Get the object property descriptor
var obj = {beep: 'boop'};
console.log(Object.getOwnPropertyDescriptor(obj, 'beep'));
//Modify the objct property descriptor
var obj = {beep: 'boop'};
Object.defineProperty(obj, 'beep', {
value: 'beep',
writable: false,
enumerable: false,
@adohe-zz
adohe-zz / Art
Last active August 29, 2015 13:56
string contain problem
public final class Art {
private static void checkRange(String s1, String s2) {
if(s1 == null || s2 == null)
throw new NullPointerException("null");
if(s1.length() == 0 || s2.length() == 0)
throw new IllegalArgumentException("zero");
if(s1.length < s2.length())
throw new IllegalArgumentException("length error");
}
@adohe-zz
adohe-zz / MasterWorker
Created February 19, 2014 03:05
master-worker model in Node.js
//Master.js
var cp = require('child_process');
var fork = require('child_process').fork;
var spawn = require('child_process').spawn;
var cpus = require('os').cpus();
//Use fork to crate new process
for(var i = 0; i < cups.length; i++) {
fork('worker.js');
}
@adohe-zz
adohe-zz / gist:9073962
Created February 18, 2014 16:10
some useful piece of code in Java
1.Get the max and min number in the array of numbers
var arr = [1, 2, 3, 4, 5];
var max = Math.max.apply(Math, arr);
var min = Math.min.apply(Math, arr);
2.Empty the array
var arr = [1, 2, 3];
arr.length = 0;
3.Don't use delete to move an item in the array
@adohe-zz
adohe-zz / gist:9053593
Created February 17, 2014 16:15
readable stream implementation in Node.js
var stream = require('stream');
var s = new Stream;
s.readable = true;
VS:
var stream = require('stream');
var Readable = stream.Readable;