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

compile conditional assigns to assign with ternary operand #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thejh
Copy link
Contributor

@thejh thejh commented Aug 4, 2011

This commit implements my suggestion from #193 and does even more. Some compilation examples:
(first line: original, second line: old compilation, third line: new compilation)

if(a)b=foo()
a&&(b=foo())
b=a?foo():b

if(a)b=foo();else b=bar()
a?b=foo():b=bar()
b=a?foo():bar()

if(a)b+=3
a&&(b+=3)
b+=a?3:0

if(a)b.c+=3
a&&(b.c+=3)
-

if(a)b.c+=3;else b.c+=4
a?b.c+=3:b.c+=4
b.c+=a?3:4

It reduces the size of minified jquery by 0,12% (116 bytes). However, it increases
the gzipped size of minified jquery by 26 bytes :( .

This commit implements my suggestion from #193. Some compilation examples:
(first line: original, second line: old compilation, third line: new compilation)

    if(a)b=foo()
    a&&(b=foo())
    b=a?foo():b

    if(a)b=foo();else b=bar()
    a?b=foo():b=bar()
    b=a?foo():bar()

    if(a)b+=3
    a&&(b+=3)
    b+=a?3:0

    if(a)b.c+=3
    a&&(b.c+=3)
    -

    if(a)b.c+=3;else b.c+=4
    a?b.c+=3:b.c+=4
    b.c+=a?3:4

It reduces the size of minified jquery by 0,12% (116 bytes). However, it **increases**
the gzipped size of minified jquery by 26 bytes :( .
@RGustBardon
Copy link
Contributor

if (0 === Math.random()) {
  _ = 0;
}
var _ = '';

if (0 === Math.random()) {
  _ += 42;
}

if (-1 !== _.indexOf('0')) {
  throw new Error();
}
!function() {
  Object.defineProperty(this, '_', {
    get: function() {
      throw new Error();
    }
  });
  if (0 === Math.random()) {
    _ += 42;
  }
}();
!function() {
  Object.defineProperty(this, '_', {
    set: function(v) {
      throw new Error();
    }
  });
  if (0 === Math.random()) {
    _ += 42;
  }
}();
with ({
  get _() {
    throw new Error();
  }
}) {
  if (0 === Math.random()) {
    _ += 42;
  }
}
with ({
  set _(v) {
    throw new Error();
  }
}) {
  if (0 === Math.random()) {
    _ += 42;
  }
}
var Foo = function() {
};

Foo.prototype.toString = function() {
  throw new Error();
};

var _ = new Foo();

if (0 === Math.random()) {
  _ += 42;
}
var Foo = function() {
};

Foo.prototype.valueOf = function() {
  throw new Error();
};

var _ = new Foo();

if (0 === Math.random()) {
  _ += 42;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants