Ternary operators in JavaScript without an “else”

First of all, a ternary expression is not a replacement for an if/else construct – it’s an equivalent to an if/else construct that returns a value. That is, an if/else clause is code, a ternary expression is an expression, meaning that it returns a value. This means several things: use ternary expressions only when you … Read more

How to log a method’s execution time exactly in milliseconds?

NSDate *methodStart = [NSDate date]; /* … Do whatever you need to do … */ NSDate *methodFinish = [NSDate date]; NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart]; NSLog(@”executionTime = %f”, executionTime); Swift: let methodStart = NSDate() /* … Do whatever you need to do … */ let methodFinish = NSDate() let executionTime = methodFinish.timeIntervalSinceDate(methodStart) print(“Execution time: \(executionTime)”) … Read more

Fastest way to convert string to integer in PHP

I’ve just set up a quick benchmarking exercise: Function time to run 1 million iterations ——————————————– (int) “123”: 0.55029 intval(“123”): 1.0115 (183%) (int) “0”: 0.42461 intval(“0”): 0.95683 (225%) (int) int: 0.1502 intval(int): 0.65716 (438%) (int) array(“a”, “b”): 0.91264 intval(array(“a”, “b”)): 1.47681 (162%) (int) “hello”: 0.42208 intval(“hello”): 0.93678 (222%) On average, calling intval() is two and … Read more