Archive for March, 2012

Ruby Benchmark String Concat versus Array Join

I was reading about string concat in ruby and found the need to do some.  before I jumped in, I ran a benchmark that was fairly surprising.  Figured I’d share.


require 'benchmark'

n = 2000000

Benchmark.bm do |x|
 str1 = "astring"
 str2 = "anotherstr"
 x.report { for i in 1..n; a = str1 + "." + str2; end }
 x.report { for i in 1..n; a = "#{str1}.#{str2}"; end }
 x.report { for i in 1..n; a = [str1, str2].join("."); end }
 x.report { for i in 1..n; a = str1 << "." << str2; end }
end

Results:


user     system      total        real
 0.880000   0.000000   0.880000 (  0.888089)
 0.920000   0.000000   0.920000 (  0.929991)
 1.640000   0.010000   1.650000 (  1.641790)
 0.700000   0.010000   0.710000 (  0.716267)

Scss Sass Opacity Plugin

I was looking for a good cross-browser plugin up front.  Here’s what I found, which is a good sass mixin for opacity.  There is probably a syntax converter, but I don’t think(?) I have one installed.  I was looking for SCSS syntax, so I converted it:

Source of Original Sass Opacity Mixin

@mixin opacity($opacity, $hover_opacity: false) {
 opacity: $opacity / 100;
 -moz-opacity: $opacity / 100;
 -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=#{$opacity})";
 filter: "alpha(opacity=#{$opacity})";
 @if $hover_opacity {
 @include opacity-hover($hover_opacity);
 }
}

@mixin opacity-hover($opacity) {
 &:hover {
 @include opacity($opacity)
 }
}

Cheers!  Leave a comment if you find an issue in a browser or know a better way!

© 2012 - Jeff Ancel
Wordpress Themes
Scroll to Top