Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just some tips #1

Closed
MaxGraey opened this issue May 28, 2019 · 1 comment
Closed

Just some tips #1

MaxGraey opened this issue May 28, 2019 · 1 comment

Comments

@MaxGraey
Copy link

@MaxGraey MaxGraey commented May 28, 2019

AssemblyScript have i32 types with full featured integer arithmetic. Math namespace usually need only for f64 and Mathf for f32.
1.

idx = <i32>Math.floor((lower + upper) / 2);

could be just:

idx = (lower + upper) / 2;

or better (also portable if you decide have uniform code for AS and JS):

idx = lower + ((upper - lower) >> 1); // eliminate overflowing when lower + upper >= 2 ** 32
function compare(v1: i32, v2: i32): i32 {
    if (v1 > v2) {
        return 1;
    } else if (v1 < v2) {
        return -1
    }
    return v1 > v2 ? 1 : v1 < v2 ? -1 : 0;
}

just:

function compare(v1: i32, v2: i32): i32 {
  return v1 - v2;
}
  1. Math.pow is pretty complex and expensive. Even for float point square numbers is better use for such simple case. But you have integers, so Math.pow completely unnecessary here:
let number = Math.pow(i, 2) as i32;

Much better:

let number = i * i
@azu

This comment has been minimized.

Copy link
Owner

@azu azu commented May 28, 2019

Thanks!

@azu azu closed this in 13af03d May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.