Posts Tagged optimization

Javascript performance

His biggest point is “do not optimize prematurely”. This is a sentiment I’ve heard a few times. Fred Brooks emphasizes it in Mythical Man Month, and Eric Raymond harps on it a bit in Art of Unix Programming.

Another point he makes is how using “weird language syntax” like double bitwise not is sometimes faster than their functional equivalents like parseInt.

And one surprising revelation (for me anyway) is that unrolled loops can sometimes be faster.

Overall, a great presentation, well worth your time if you work with javascript a lot.

Tags: , , ,

Magical PHP JSON Object Cleaner

I wrote this method the other day that takes a simple PHP object, inspects it’s properties and “prunes” empty ones. I wrote this method in order to compress JSON objects by removing null properties before sending them down the wire, a big problem when using base objects or models.

If you find this useful or a have a suggestion, feel free to let me know!

private function getStripped($obj) {
		$objVars = get_object_vars($obj);

		if(count($objVars) > 0) {
			foreach($objVars as $propName => $propVal) {
				if(gettype($propVal) == "object") {
					$cObj = $this->getStripped($propVal);
					if($cObj == null) {
						unset($obj->$propName);
					} else {
						$obj->$propName = $cObj;
					}
				} else {
					if(empty($propVal)) {
						unset($obj->$propName);
					}
				}
			}
		} else {
			return null;
		}
		return $obj;
	}

Tags: , , ,