flexxui / pscript

Python to JavaScript compiler
http://pscript.readthedocs.io
BSD 2-Clause "Simplified" License
260 stars 25 forks source link

Support `base` arg of `int` (parseInt way) #33

Closed Winand closed 6 years ago

Winand commented 6 years ago

Can convert expressions with specified radix:

int('0xab', 0)  # =171 (base is got from expression)
# int('0b0101', 0) is not supported
int('ab', 16)  # =171

fixes #21

P. S. int() returns 0 in Python and NaN in pscript

almarklein commented 6 years ago

Let's benchmark this:

<html>
<body>
<script>

var N = 1000000;
var t0;

function int1(x, base) {
    if(base !== undefined) return parseInt(x, base);
    return x<0 ? Math.ceil(x): Math.floor(x);
}
function int2(x, base) {
    return parseInt(x, base);
}

t0 = performance.now();
for (var i=-N; i<N; i++) {
    int1(i);
}
console.log(performance.now() - t0);

t0 = performance.now();
for (var i=-N; i<N; i++) {
    int2(i);
}
console.log(performance.now() - t0);

</script>
</body>
</html>

On Chrome the results are more or less equal. On FF the first (as in current) implementation is much faster.

almarklein commented 6 years ago

Thanks!