Skip to content

Instantly share code, notes, and snippets.

@marcaube
marcaube / outbound.js
Last active August 29, 2015 13:56
Track outbound links using google analytics. This script pushes an event when clicking on <a> tags with the `js-ext` class.
/**
* Utility to wrap the different behaviors between W3C-compliant browsers
* and IE when adding event handlers.
*
* @param {Object} elem The element on which to attache the event listener
* @param {string} event The event type to listen for (e.g. load, click, etc.)
* @param {function()} callback The callback function that receives the notification
*/
function addListener(elem, event, callback) {
// W3C

US/CAN phone regex

A simple regex I wrote to loosely validate phone number format. I want it to match a US/CAN phone number without being anal about it and alienating my users with a rigit format. It will match any of those:

  • 1-800-555-5555
  • 1 (800) 555-5555
  • 1 555 555 5555
  • 555-555-5555
  • 555 555-5555
  • 555 555 5555
@marcaube
marcaube / gist:9529442
Created March 13, 2014 14:22
Nice commit graph
git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
@marcaube
marcaube / is_array_benchmark.php
Last active August 29, 2015 14:05
Benchmarking "(array) $var === $var" vs "is_array($var)" for speed
<?php
/**
* @param array $functions An associative array of closures to benchmark
* @param int $iterations The number of iterations
*/
function benchmark($functions, $iterations)
{
foreach ($functions as $name => $function) {
$start = microtime(true);
@marcaube
marcaube / strlen_benchmark.php
Created January 22, 2015 21:34
Benchmarking "strlen($string) > 25" vs "isset($string[25])" for speed
<?php
/**
* @param array $functions An associative array of closures to benchmark
* @param int $iterations The number of iterations
*/
function benchmark($functions, $iterations)
{
foreach ($functions as $name => $function) {
$start = microtime(true);
### Keybase proof
I hereby claim:
* I am marcaube on github.
* I am marcaube (https://keybase.io/marcaube) on keybase.
* I have a public key whose fingerprint is A323 88CD 3850 262E 1EA3 CC00 0A1A 7489 29EB 0696
To claim this, I am signing this object:
@marcaube
marcaube / bootstrap-hugrid.js
Created March 26, 2012 14:01
Twitter Bootstrap Heads-Up Grid
definegrid = function() {
var browserWidth = $(window).width();
// LARGE DESKTOP & UP
if (browserWidth >= 1200)
{
pageUnits = 'px';
colUnits = 'px';
pagewidth = 1170;
columns = 12;
@marcaube
marcaube / vendors
Created September 19, 2012 12:52 — forked from ubermuda/vendors.php
A symfony2 vendors script hacked to handle svn urls
#!/usr/bin/env php
<?php
/*
* This file is part of the Symfony Standard Edition.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
@marcaube
marcaube / blog-performance-et-pubs.md
Last active December 16, 2015 00:49
Performance web et publicités

Préambule

Ces derniers temps, j'ai été confronté à un problème de performance sur le site d'un client. Le site est développé avec Symfony2, utilise l'ORM Doctrine2, il est responsive, utilise adaptive-images pour servir des images optimisées, etc etc.

Jusque là, le site avait été optimisé pour mettre en cache les requêtes à la base de données (Query & Result cache), mettre en cache des fragment de template (avec les ESI) et utiliser au mieux les entêtes HTTP pour mettre en cache les pages du côté client.

Les résultats de ces mises en cache granulaires était un temps de chargement entre 60ms pour une cache navigateur primée et ~150-200ms quand il y avait un hit sur l'application, parce que la plupart du temps il n'y avait aucun hit sur la base de données.

Le problème

Le problème est arrivé quand le client a décidé d'ajouter des publicités sur son site. Ne voulant pas utiliser un ad-network existant, le cl

@marcaube
marcaube / watchr_script.rb
Created August 28, 2013 14:59
Automatically generate styleguide with styledocco from less files with the watchr gem. Lauch with `$ watchr watchr_script.rb`
watch('(.*).less') do |match|
system("styledocco")
end