Skip to content

Instantly share code, notes, and snippets.

@gmilby
gmilby / php_server_on_demand.php
Created July 3, 2013 14:56
'trying' to make a script to execute by script... definitely note safe, but it doesn't matter - it doesn't work :-P
<?php
function exec_local_url($url) {
exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/'
. addslashes($url) . '" >/dev/null 2>&1'
);
}
exec_local_url('php -S localhost:1000 -t .');
// exec(php -S localhost:1000);
@gmilby
gmilby / findValueByKey.js
Created July 8, 2013 00:56
underscore - find value by key
_.mixin({
findKeysByValue : function(object,value) {
var retVal=[]
for(var key in object){
var obj=object[key]
if(obj==value){
retVal.push(key)
}
}
return retVal;
@gmilby
gmilby / selectOptionPulldowndotEach.js
Created July 8, 2013 01:18
use jquery & .each( to get values of selected items in a pulldown
$('#select > option').each(function() {
alert($(this).text() + ' ' + $(this).val());
});
$('#select > option:selected').each(function() {
alert($(this).text() + ' ' + $(this).val());
});
// This function will return an array of text/value pairs for the selects matching the given class.
@gmilby
gmilby / mysqlcreatetable.sql
Created July 10, 2013 15:13
my first command line table... i've been doing this 10 years - never made a table on command line in mysql...
mysql> CREATE TABLE messages (id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NULL,
text VARCHAR(255),
created_at INT(11),
updated_at INT(11),
PRIMARY KEY (id)) ENGINE=INNODB;
Query OK, 0 rows affected (0.05 sec)
mysql>
/*
Loading dot dot dot
*/
body {
padding: 100px;
font-size: 62.5%;
}
.loading {
@gmilby
gmilby / scrubberFunc.php
Created July 18, 2013 15:20
good small sanitizer
// move to helper
// clean submitted data before handling in db realm (roll 32 sided di now...)
function scrubberUpper($str) { return filter_var(strtoupper(trim($str)), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); }
class SimpleCounter {
protected $total = 0;
public function add($amount)
$this->total += $amount
return $this
}
public function subtract($amount)
@gmilby
gmilby / forLoops.php
Created July 19, 2013 22:40
speed tests for "for loops"
Syntax A - 0.25367754 seconds
for($i=0; $i<1000000; $i++){}
Syntax B - 0.25415988 seconds
for($i=0; $i<=999999; $i++){}
Syntax C - 0.25957097 seconds
for($i=0; $i!=1000000; $i++){}
@gmilby
gmilby / whileLoops.php
Created July 19, 2013 22:42
performance test on while loops
Syntax A - 0.25400330 seconds
$i = 0 ;
while($i<1000000){ $i++ ; }
Syntax B - 0.25518957 seconds
$i = 0 ;
while($i<=999999){ $i++ ; }
Syntax C - 0.26488042 seconds
@gmilby
gmilby / performanceTips1.js
Created July 19, 2013 22:49
performance tips for jquery
1. Beware of class-only selectors
jQuery
A great part about using a JavaScript library like jQuery is its selectors. But before you use them all over the place, stop to think: what are the processing costs?
Basically, jQuery defines all the elements on a page as a huge JavaScript object, and then interacts with it. You don’t have to look into the source code to realize that the selector function works by iterating all objects that fit a certain criteria. Therefore, it makes sense to give the selection function a helping hand by defining the selection criteria more specifically.
Let’s do a quick case-study:
Pretend you’re doing site navigation and want to select an LI that has the CSS class ‘active.’ One way to select this element is:
$('.active')
This selector works just fine, but think about what the jQuery DOM traversal has to do here: iterating through hundreds of elements on the page, selecting any with the class ‘active.’ That’s a lot of processing for our little nav bar.
It turns out, according to selec