Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
@WebReflection
WebReflection / AnimationBatch.js
Last active August 29, 2015 14:06
A generic requestAnimationFrame batches controller
var AnimationBatch = AnimationBatch || (function(window){
'use strict';
/*! (C) Andrea Giammarchi - Mit Style License */
var
instances = [],
forEach = instances.forEach || function (fn) {
// ad-hoc shim for this use case
var
self = this,
@WebReflection
WebReflection / Object.prototype.watch.js
Last active September 15, 2016 15:38
There is a reason Firefox still uses `Object.prototype.watch` … it's freaking useful if you know how to use it.
(function(Object){
/*! (C) Andrea Giammarchi - Mit Style License */
/////////////////////////////////////////////////////////////////
//
// compatibility
// native: Firefox Desktop and Mobile
// partial: IE8 works with DOM nodes only
// simulated: all other browsers except IE < 8
@WebReflection
WebReflection / gist:88056251c8c9ebbf6eaa
Last active February 15, 2016 16:00
setTimeout and setInterval implicitly pass the global context (explicit via specs)

I run into a curios unexpected behavior, where I could not prevent polluting the global context.

Apparently this is specified as such but I've no idea why anyone would expect to receive a global context when "use strict" is used trying to avoid it.

Here the quick sumamry

(function () {
  'use strict'; // <== as a developer, I don't want implicit window
 setTimeout(function () {
@WebReflection
WebReflection / hashCode.js
Created July 2, 2014 05:21
micro JavaScript hashCode Java String like implementation
function hashCode(s){for(var h=0,i=0;i<s.length;h&=h){h=31*h+s.charCodeAt(i++)}return h}
// or
String.prototype.hashCode=function(){for(var h=0,i=0;i<this.length;h&=h){h=31*h+this.charCodeAt(i++)}return h};
@WebReflection
WebReflection / nody
Last active August 29, 2015 14:02
nody is a little CDN like script designed for low memory capable devices and read-only intranets
#!/usr/bin/env node
/**
* ___ ___ ___ ___
* /\__\ /\ \ /\ \ |\__\
* /::| | /::\ \ /::\ \ |:| |
* /:|:| | /:/\:\ \ /:/\:\ \ |:| |
* /:/|:| |__ /:/ \:\ \ /:/ \:\__\ |:|__|__
* /:/ |:| /\__\ /:/__/ \:\__\ /:/__/ \:|__| /::::\__\
* \/__|:|/:/ / \:\ \ /:/ / \:\ \ /:/ / /:/~~/~
@WebReflection
WebReflection / fast-bind.js
Last active March 27, 2021 18:29
Function.prototype.bind performance problem solved in JS … (at least for 90% of common cases without partial args)
/*jslint indent: 2 */
(function (FunctionPrototype) {
'use strict';
var originalBind = FunctionPrototype.bind;
if (!originalBind.patched) {
Object.defineProperty(
FunctionPrototype,
'bind',
{
configurable: true,
ctor->defineDefaultProperty(QStringLiteral("setPrototypeOf"), method_set_proto_of, 2);
ReturnedValue ObjectPrototype::method_set_proto_of(CallContext *ctx)
{
Scope scope(ctx);
Scoped<Object> o(scope, ctx->argument(0));
Scoped<Object> p(scope, ctx->argument(1));
if (!o)
return ctx->throwTypeError();
o->method_set_proto(p);
@WebReflection
WebReflection / dollar.js
Last active August 29, 2015 14:01
A jQuery like function with :first built in optimization, will return an Array regardless it's one element search only for consistency.
function $(CSS, parentNode) {
// @link http://webreflection.blogspot.com/2014/05/134-bytes-for-optimized-and-very-basic.html
var el = parentNode || document,
first = CSS.lastIndexOf(':first') === CSS.length - 6,
query = first ?
el.querySelector(CSS.slice(0, -6)) :
el.querySelectorAll(CSS);
return first ?
(query ? [query] : []) :
Array.prototype.slice.call(query);
@WebReflection
WebReflection / ToPropertyDescriptor.md
Last active August 29, 2015 14:01
ToPropertyDescriptor ( Obj )

proposal identical to current ES5-6 one except [[HasOwnProperty]] is used instead

ToPropertyDescriptor

When the abstract operation ToPropertyDescriptor is called with object Obj, the following steps are taken:

  1. ReturnIfAbrupt(Obj).
  2. If Type(Obj) is not Object throw a TypeError exception.
  3. Let desc be a new Property Descriptor that initially has no fields.
  4. If HasOwnProperty(Obj, "enumerable") is true, then
    1. Let enum be Get(Obj, "enumerable").
  5. [Retur
@WebReflection
WebReflection / Object.assign.js
Last active November 1, 2016 06:32
ES3 compatible Object.assign
if (!('assign' in Object)) {
Object.assign = function(has){
'use strict';
return assign;
function assign(target, source) {
for (var i = 1; i < arguments.length; i++) {
copy(target, arguments[i]);
}
return target;
}