Skip to content

Instantly share code, notes, and snippets.

View thewarpaint's full-sized avatar
🎄
https://dosmilveinte.mx

Eduardo Garcia thewarpaint

🎄
https://dosmilveinte.mx
View GitHub Profile
@zeusdeux
zeusdeux / socketio.md
Last active March 18, 2020 22:51
Vanilla websockets vs socket.io

Vanilla websockets vs socket.io

Note: The points below are a comparison of using vanilla websockets on client and server and using 'em via socket.io and not about why only use socket.io

  • Native websockets provide us with only a send method to send data to the server. Send accepts only string input (not too sure about this). Socket.io lets us emit arbitrary events with arbitrary data (even binary blobs) to the server.
  • To receive messages on a vanilla websocket you can only assign a handler for the message event. The data you receive is mostly likely to be text (again not too sure about this) and you will have to parse it manually before consuming it. Socket.io lets the server and client both emit arbitrary events and handles all the parsing and packing/unpacking.
  • Socket.io gives us both a server and a client. Implementing a vanilla websocket server isn't something that you would want to do per project since it's quite painful. You will have to implement [RFC6455](https://tools.ietf.org/h
@xk
xk / happyNumbers.js
Created February 10, 2014 23:40
Concise Happy Numbers JavaScript
//2014-02-10 [email protected] concise happy numbers JavaScript
function happy (n) {
var past= [];
while (n= [].reduce.call(n.toString(), function (a,n) { return a+n*n }, 0))
if (n === 1) return 1; else if (past.indexOf(n) >= 0) return 0; else past.push(n);
}
for (var i=0 ; i<=100 ; ++i) happy(i) && console.log(i);
@thomseddon
thomseddon / gist:3511330
Last active March 8, 2023 03:39
AngularJS byte format filter
app.filter('bytes', function() {
return function(bytes, precision) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
if (typeof precision === 'undefined') precision = 1;
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
});