<commit_before><!doctype html>

<html lang="en" prefix="og: http://ogp.me/ns#">
  <head>
    <meta charset="utf-8">
    <meta name="author" content="<NAME>">
    <meta name="keywords" content="scala,java,benchmark">

    <link rel="shortcut icon" href="/media/images/favicon.ico">
    <link rel="stylesheet" href="/assets/css/blog.css" type="text/css" media="screen" title="no title" charset="utf-8">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:creator" content="@skaalf">
    <meta property="og:title" content="Scala is faster than Java">
    <meta property="og:description" content="Benchmarking Scala and Java.">
    <meta property="og:image" content="">

    <title>Scala is faster than Java</title>
  </head>
  <body>
    <div class="container">
      <div class="nav">
        <a href="/" class="logo">
          <em class="hi">hi!</em>
          <span class="me">I'm Julien</span>
        </a>
        <ul class="social">
          <li><a href="https://twitter.com/skaalf">&#xf099;</a></li>
          <li><a href="https://github.com/jto">&#xf09b;</a></li>
          <li><a href="http://www.linkedin.com/in/julientournay">&#xf0e1;</a></li>
        </ul>
      </div>
      <div class="content">
        <article itemscope itemtype="http://schema.org/Article">
  <!-- <time>27 April 2014</time> -->
  <a href="https://twitter.com/share" data-text="Scala is faster than Java" data-hashtags="scala,java,benchmark" data-via="skaalf" class="twitter-share-button" data-lang="en">Tweet</a>
  <h1 itemprop="name">
    Scala is faster than Java
  </h1>
  <div itemprop="articleBody">
  <p><header>
Scala is generally considered slower than Java, especially when it's used in a functional style. This article explains why this generally accepted assumption is just wrong.
</header></p>

<h2>Data validation</h2>

<p>One of the very common problem in programming is data validation. In a word, we want to make sure that every incoming data has the correct structure. We need to discriminate unsafe external inputs from safe, compiler validated data. In a typical web application, you need do that on every request. Surely, it could have an impact on the performances of your application. In this article, I'm going to compare two extremely different solutions addressing the issue. The Bean validation API from Java, and the unified validation API from play. The latter being a more functional approach, featuring immutability and typesafety.</p>

<h3>Java: The Bean Validation API, aka JSR 303</h3>

<p>The bean validation <a href="https://jcp.org/en/jsr/detail?id=303">specification</a> was first released in 2009. This API uses annotations to set constraints on <em>Java Beans</em>. You then call the validation method on an annotated instance to find out if its valid. The reference, and most popular implementation is from <a href="http://hibernate.org/validator/">Hibernate</a>.</p>

<p>Here's a little example from their website:</p>

<script src="https://gist.github.com/jto/5ec094ab4fd40f699b88.js?file=1_example.java"></script>


<p>That's for the declaration. The actual validation looks like this (again, from their website):</p>

<script src="https://gist.github.com/jto/5ec094ab4fd40f699b88.js?file=2_example2.java"></script>


<p>So you pass an instance to <code>validator.validate</code> and you get a <code>Set</code> of errors. If the Set is empty, the object was valid.</p>

<p>Most of the time, you'll use this API to validate Json or XML. Here's how you'd unmarshall and validate a json object:</p>

<script src="https://gist.github.com/jto/5ec094ab4fd40f699b88.js?file=3b_json.java"></script>


<h3>Scala: Play unified validation API.</h3>

<p><a href="https://github.com/jto/validation">The unified validation API</a> is an effort to provide the core primitives needed to validate any data structure. Its primary intent is to replace the Json validation API and the form validation API in play framework. It's easy to extend, and support Json validation, and form validation out of the box.</p>

<p>Here's a port of the Json validation scenario. Note that this time, we are validating the Json directly.</p>

<script src="https://gist.github.com/jto/5ec094ab4fd40f699b88.js?file=3_example.scala"></script>


<p>When implementing the API, I didn't focus much on performances. My primary objectives were correctness, compositionality, and typesafety. Actually some design choices, like making every validator lazily evaluated, have a negative impact on performances. Let's how it performs.</p>

<h2>Benchmark</h2>

<h3>The test protocol</h3>

<p>The benchmark consists of parsing and validating Json objects stored in a file. The data are extracted from <a href="http://labrosa.ee.columbia.edu/millionsong/lastfm">the Last.fm Dataset</a>. The JSON structure has been modified a bit for easier parsing (using the awesome <a href="http://stedolan.github.io/jq/">jq</a>). The code is hosted <a href="https://github.com/jto/validation-bench">here</a></p>

<p>The performances are measured using a series of scalameter micro-benchmarks. Both APIs are using Jackson for parsing JSON.</p>

<h3>Results</h3>

<p>The benchmark measures the time taken to parse and validate from 5000 to 10000 json objects.
Two different scenario are tested:</p>

<ul>
<li>All the objects are valid</li>
<li>All the objects have an invalid field</li><commit_msg>typos<commit_after><!doctype html>

<html lang="en" prefix="og: http://ogp.me/ns#">
  <head>
    <meta charset="utf-8">
    <meta name="author" content="<NAME>">
    <meta name="keywords" content="scala,java,benchmark">

    <link rel="shortcut icon" href="/media/images/favicon.ico">
    <link rel="stylesheet" href="/assets/css/blog.css" type="text/css" media="screen" title="no title" charset="utf-8">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:creator" content="@skaalf">
    <meta property="og:title" content="Scala is faster than Java">
    <meta property="og:description" content="Benchmarking Scala and Java.">
    <meta property="og:image" content="">

    <title>Scala is faster than Java</title>
  </head>
  <body>
    <div class="container">
      <div class="nav">
        <a href="/" class="logo">
          <em class="hi">hi!</em>
          <span class="me">I'm Julien</span>
        </a>
        <ul class="social">
          <li><a href="https://twitter.com/skaalf">&#xf099;</a></li>
          <li><a href="https://github.com/jto">&#xf09b;</a></li>
          <li><a href="http://www.linkedin.com/in/julientournay">&#xf0e1;</a></li>
        </ul>
      </div>
      <div class="content">
        <article itemscope itemtype="http://schema.org/Article">
  <!-- <time>27 April 2014</time> -->
  <a href="https://twitter.com/share" data-text="Scala is faster than Java" data-hashtags="scala,java,benchmark" data-via="skaalf" class="twitter-share-button" data-lang="en">Tweet</a>
  <h1 itemprop="name">
    Scala is faster than Java
  </h1>
  <div itemprop="articleBody">
  <p><header>
Scala is generally considered slower than Java, especially when it's used in a functional style. This article explains why this generally accepted assumption is just wrong.
</header></p>

<h2>Data validation</h2>

<p>One of the very common problem in programming is data validation. In a word, we want to make sure that every incoming data has the correct structure. We need to discriminate unsafe external inputs from safe, compiler validated data. In a typical web application, you need do that on every request. Surely, it could have an impact on the performances of your application. In this article, I'm going to compare two extremely different solutions addressing the issue. The Bean validation API from Java, and the unified validation API from play. The latter being a more functional approach, featuring immutability and typesafety.</p>

<h3>Java: The Bean Validation API, aka JSR 303</h3>

<p>The bean validation <a href="https://jcp.org/en/jsr/detail?id=303">specification</a> was first released in 2009. This API uses annotations to set constraints on <em>Java Beans</em>. You then call the validation method on an annotated instance to find out if its valid. The reference, and most popular implementation is from <a href="http://hibernate.org/validator/">Hibernate</a>.</p>

<p>Here's a little example from their website:</p>

<script src="https://gist.github.com/jto/5ec094ab4fd40f699b88.js?file=1_example.java"></script>


<p>That's for the declaration. The actual validation looks like this (again, from their website):</p>

<script src="https://gist.github.com/jto/5ec094ab4fd40f699b88.js?file=2_example2.java"></script>


<p>So you pass an instance to <code>validator.validate</code> and you get a <code>Set</code> of errors. If the Set is empty, the object was valid.</p>

<p>Most of the time, you'll use this API to validate Json or XML. Here's how you'd unmarshall and validate a json object:</p>

<script src="https://gist.github.com/jto/5ec094ab4fd40f699b88.js?file=3b_json.java"></script>


<h3>Scala: Play unified validation API.</h3>

<p><a href="https://github.com/jto/validation">The unified validation API</a> is an effort to provide the core primitives needed to validate any data structure. Its primary intent is to replace the Json validation API and the form validation API in play framework. It's easy to extend, and support Json validation, and form validation out of the box.</p>

<p>Here's a port of the Json validation scenario. Note that this time, we are validating the Json directly.</p>

<script src="https://gist.github.com/jto/5ec094ab4fd40f699b88.js?file=3_example.scala"></script>


<p>When implementing the API, I didn't focus much on performances. My primary objectives were correctness, compositionality, and typesafety. Actually some design choices, like making every validator lazily evaluated, have a negative impact on performances. Let's see how it performs.</p>

<h2>Benchmark</h2>

<h3>The test protocol</h3>

<p>The benchmark consists of parsing and validating Json objects stored in a file. The data are extracted from <a href="http://labrosa.ee.columbia.edu/millionsong/lastfm">the Last.fm Dataset</a>. The JSON structure has been modified a bit for easier parsing (using the awesome <a href="http://stedolan.github.io/jq/">jq</a>). The code is hosted <a href="https://github.com/jto/validation-bench">here</a>.</p>

<p>The performances are measured using a series of scalameter micro-benchmarks. Both APIs are using Jackson for parsing JSON.</p>

<h3>Results</h3>

<p>The benchmark measures the time taken to parse and validate from 5000 to 10000 json objects.
Two different scenario are tested:</p>

<ul>
<li>All the objects are valid</li>
<li>All the objects have an invalid field</li>
{'fraction_non_alphanumeric': 0.13188958081606098, 'fraction_numerical': 0.01877497908727577, 'mean_word_length': 4.3988961364776715, 'pattern_counts': {'":': 0, '<': 247, '<?xml version=': 0, '>': 247, 'https://': 20, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '968496', 'n_tokens_mistral': 3417, 'n_tokens_neox': 3194, 'n_words': 1162}