at-import / Sassy-Strings

Advanced string handling for Sass
41 stars 3 forks source link

str-replace as a while loop #4

Closed Snugug closed 10 years ago

Snugug commented 10 years ago

Don't remember why I wrote it this way instead of as a recursive function, but something to look at.

@function str-replace($string, $search, $replace) {
  $length: str-length($replace);
  $index: str-index($string, $search);
  $slice: $string;
  @while $index != 0 {
    $slice: str-slice($slice, 0, $index - 1);
    $slice: $slice + $replace;
    $slice: $slice + str-slice($string, $index + $length);
    $index: str-index($slice, $search);
  }
  @return $slice;
}
KittyGiraudel commented 10 years ago

We should probably run tests to see which one is faster but from what I know while loops perform poorly in Sass. :(

Snugug commented 10 years ago

Having done a quick test, the recursive function currently in there compiles 100 multi-depth string replaces in ~.85s, whereas the while loop does the same in ~0.06s. There is currently a logic issue somewhere in the while loop, and it doesn't support case sensitivity (both of which would need to be added/fixed), but it's significantly faster than what is written.

On the overall code review side, I would like to see some performance benchmarks for each of the functions currently written. str-replace is intensive, but as written it's also very slow. As we've seen over the past few projects we've shared code on, taking a different approach to the same problem has the potential to dramatically increase performance, and I'd like to see what the base performance is for each of these and see if it can be improved.

KittyGiraudel commented 10 years ago

Hey, very good to know Sam. I wonder why the recursive edition performs so slowly compared to the while loop.

Do you have a solution to efficiently measure compilation time?

Snugug commented 10 years ago

compass compile --time

KittyGiraudel commented 10 years ago

I was wondering if there was some kind of a Grunt task for this.

jamiebuilds commented 10 years ago

@HugoGiraudel SassMeister does include a compilation time if you ever want to do quick checks