Removing duplicate characters

An interesting string method was brought to my attention on the ruby-talk mailing list: string.squeeze. It removes duplicate characters in a string, and performs better than regular expressions or splitting-joining, to boot. Craig posted some benchmarks:

require 'benchmark'

n = 1_000_000

Benchmark.bm(10) do |x|
 x.report("gsub") { n.times do
    "This     is  a test   string.".gsub(/ +/, ' ')
  end
 }
 x.report("gsub!") { n.times do
    "This     is  a test   string.".gsub!(/ +/, ' ')
  end
 }
 x.report("split.join") { n.times do
    "This     is  a test   string.".split.join(' ')
  end
 }
 x.report("squeeze.strip") { n.times do
    "This     is  a test   string.".squeeze(' ').strip
  end
 }
end

               user     system      total        real
gsub        4.470000   0.040000   4.510000 (  4.578173)
gsub!       4.390000   0.020000   4.410000 (  4.468695)
split.join  3.500000   0.020000   3.520000 (  3.556669)
squeeze.strip  1.980000   0.010000   1.990000 (  2.003622)

Nice!

Make it nice for Andoid and iPhones

Mobile Webkit recognizes some useful CSS media queries, which I’m using to include a special mobile stylesheet on my page:

<link href="/style/mobile.css" rel="stylesheet" media="only screen and (max-device-width: 480px)" type="text/css" />

The key here being the media attribute value of only screen and (max-device-width: 480px). The mobile stylesheet contains just a few rules to make things more readable and space efficient for small screens, like reduced padding, etc.

The Android SDK contains an Android emulator which I use to test the page in a browser (Safari should look about identical to Android’s browser.)

The media queries work well for including tweaks, but if you’re hiding a bunch of extra content for mobile, you’d be better off serving a special mobile version of the page without the extra content to save bandwidth.