Skip to content

Commit

Permalink
[sass] Improve compilation speed using a different pow implementation (
Browse files Browse the repository at this point in the history
…#5674)

This commit reduces the SASS compilation times using different math.pow implementation code.

Co-authored-by: Greg Thompson <[email protected]>
  • Loading branch information
markov00 and thompsongl authored Mar 3, 2022
1 parent 288eb70 commit 20592c1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Added `textWrap` to `EuiSelectableListItem`, `EuiSelectableList`, and `EuiSelectable.listOptions` ([#5679](https://github.com/elastic/eui/issues/5679))
- Forced `truncation` on `EuiSuggest` items when `isVirtualize` ([#5679](https://github.com/elastic/eui/issues/5679))
- Changed proportion handling between content and image in `horizontal` `EuiEmptyPrompt` and added spacing between ([#5663](https://github.com/elastic/eui/pull/5663))
- Reduced SASS compilation time using a different math `pow` implementation ([#5674](https://github.com/elastic/eui/pull/5674))

**Bug fixes**

Expand Down
64 changes: 1 addition & 63 deletions src/global_styling/functions/_math.scss
Original file line number Diff line number Diff line change
@@ -1,63 +1 @@
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow
@function pow($number, $power) {
@if ($power != $power or ($power != 0 and $power == $power / 2)) {
@return $power;
}
@if ($power == 1) {
@return $number;
}
@if ($power == 0) {
@return 1;
}
@if ($power < 0) {
@return 1 / pow($number, -$power);
}
@if (0 < $power and $power < 2) {
$hasLeadingOne: false;
@if (floor($power) == 1) {
$power: $power - 1;
$hasLeadingOne: true;
} @else {
$hasLeadingOne: false;
}
$doublePower: pow($number, $power * 2);
$fullPower: nthRoot($doublePower, 2);
@if ($hasLeadingOne) {
$fullPower: $fullPower * $number;
}
@return $fullPower;
} @else if (getDecimal($power) != 0) {
$wholePower: floor($power);
$decimalPower: getDecimal($power);
@return pow($number, $wholePower) * pow($number, $decimalPower);
} @else {
$hasTrailingOne: $power % 2 == 1;
@if ($hasTrailingOne) {
$power: $power - 1;
}
$halfPower: pow($number, floor($power / 2));
$fullPower: $halfPower * $halfPower;
@if ($hasTrailingOne) {
$fullPower: $fullPower * $number;
}
@return $fullPower;
}
}

@function getDecimal($number) {
@if ($number < 0) {
$number: -$number;
}
@return $number % 1;
}

// From: http://rosettacode.org/wiki/Nth_root#JavaScript
@function nthRoot($num, $n: 2, $prec: 12) {
$x: 1;

@for $i from 0 through $prec {
$x: 1 / $n * (($n - 1) * $x + ($num / pow($x, $n - 1)));
}

@return $x;
}
@import 'math_pow';
82 changes: 82 additions & 0 deletions src/global_styling/functions/_math_pow.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
The MIT License (MIT)
Copyright (c) 2015 strarsis https://github.com/strarsis/sass-math-pow
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

@function pow($number, $exp) {
$exp1: round($exp);
$result: powInt($number, $exp1);

@if ($exp1 != $exp) {
$result: $result * mathExp(($exp - $exp1) * mathLn($number));
}

@return $result;
}

@function powInt($number, $exp) {
@if $exp == 0 {
@return 1;
} @else if $exp < 0 {
@return 1 / powInt($number, -$exp);
} @else {
$e: floor($exp / 2);
$pow: pow($number, $e);
@if $e * 2 == $exp {
@return $pow * $pow;
} @else {
@return $pow * $pow * $number;
}
}
}

@function mathExp($value) {
$item: 1;
$result: 1;

@for $index from 1 to 100 {
$item: $item * $value / $index;
$result: $result + $item;
}

@return $result;
}

@function mathLn($value) {
$tenExp: 0;
$lnTen: 2.30258509;

@while ($value > 1) {
$tenExp: $tenExp + 1;
$value: $value / 10;
}

$item: -1;
$result: 0;

@for $index from 1 to 100 {
$item: $item * (1 - $value);
$result: $result + $item / $index;
}

@return $result + $tenExp * $lnTen;
}

0 comments on commit 20592c1

Please sign in to comment.