Skip to content

Instantly share code, notes, and snippets.

@bwg
bwg / dnsmasqresolv.sh
Created June 1, 2012 17:30
Shell script for updating resolv.dnsmasq.conf in OS X
#!/bin/bash
# updates resolv.dnsmasq.conf with DHCP provided DNS servers for the
# currently active network interface on OS X
# the location of the dnsmasq resolv file
RESOLV=/etc/resolv.dnsmasq.conf
# get the list of active services from scutil
SERVICES=$(echo 'show Setup:/Network/Global/IPv4' | scutil | grep -e '[[:digit:]] :' | awk '{print $3}')
@bwg
bwg / serializetest
Last active December 1, 2020 00:03
testing serialize vs. json_encode vs. var_export vs. msgpack_pack
PHP Version 5.4.30
--------------------------------------------------
Testing 10 item array over 1,000 iterations:
serialize 2.1979808807373 ms
json_encode 1.3420581817627 ms
var_export 1.9409656524658 ms
msgpack_pack 1.5850067138672 ms
--------------------------------------------------
@bwg
bwg / selection.js
Last active December 18, 2015 15:39
cross browser double click selection/range fun
// Browsers behave differently when dbl clicking to select
// a range.
//
// Chrome and IE use the text node that contains the selected text
// as the startContainer with an offset to exclude any whitespace
// characters at the start of the node.
//
// Firefox will use a text node *before* the selected text
// as the startContainer, with a positive offset set to the end
// of the node. If there is no previous sibling of the selected text
@bwg
bwg / widget-parent-renderqueue.js
Created January 24, 2011 16:02
plugin for widgetparent that uses an async-queue to render children
YUI.add('sm-widget-parent-renderqueue', function(Y) {
/**
* Plugin for WidgetParent that uses an async-queue to render children
*
* @module widget-parent-render-queue
*/
var NAME = 'renderqueue',
HOST = 'host',
@bwg
bwg / arraykeytest.php
Last active August 29, 2015 14:13
array_key_exists vs. isset
<?php
$iterations = 100000;
$value = [
'one' => 1,
'two' => 2,
'three' => 3,
];
@bwg
bwg / ctortest.php
Last active August 29, 2015 14:11
comparing subclasses that call parent constructors vs. cloning empty object
<?php
$iterations = 100000;
class zero {
public static function one() {
static $empties = [];
$empty = &$empties[static::class];
@bwg
bwg / noticetest
Last active August 29, 2015 14:03
is checking an array_key faster than disabling E_NOTICE
PHP Version 5.4.30
-------------------------------------------------------
Testing 10 item array over 100,000 iterations:
isset 18.280982971191
array_key_exists 96.060037612915
^E_NOTICE 71.103811264038
-------------------------------------------------------
Testing 100 item array over 100,000 iterations:
@bwg
bwg / hashtest
Created July 10, 2014 04:58
md5 vs. sha1 vs. crc32
PHP Version 5.4.30
--------------------------------------------------
Testing 0 char string over 100,000 iterations:
md5 113.2709980011 ms
sha1 117.16985702515 ms
crc32 78.847885131836 ms
--------------------------------------------------
Testing 1 char string over 100,000 iterations:
@bwg
bwg / passbyreferencetest
Last active August 29, 2015 14:03
testing pass by reference vs. pass by value
PHP Version 5.4.29
--------------------------------------------------
Testing large_array over 100,000 iterations:
pass_by_ref 95.081090927124 ms
pass_by_val 86.126089096069 ms
--------------------------------------------------
Testing small_array over 100,000 iterations:
@bwg
bwg / arraydifftest
Last active August 29, 2015 14:03
array_diff_key() vs. loop/unset
PHP Version 5.4.30
-------------------------------------------------------
Testing 10 item array over 1,000 iterations:
array_diff_key 1.2109279632568
loop/unset 0.87404251098633
-------------------------------------------------------
Testing 100 item array over 1,000 iterations: